Get your OVH nic public info with the OVH SOAP API and WsdlToPhp

  15 June 2013       This post has 0 comment       By Mikaël DELSOL

Requirements

Your server must run the latest PHP version with the native SoapClient class.

You first need to download the generated package from this page. The generated package gathers classes by their type (Service, Struct and Enum) so you can quickly find a class. It includes :

  • the sample file: the file that shows you how to start using the generated package
  • the autoload file: the file which loads all the generated class with the good path
  • the main WsdlClass class: the class from which each generated class inherits in order to inherit usefull methods and generic methods to deal with the SoapClient
  • the ClassMap class: the class which defines the mapping between native OVH SOAP structs and generated classes
  • all the classes required to communicate with the OVH SOAP API

If you have a good IDE, then it should be really easy to navigate trough all these numerous classes.

A simple case: get your nic public info

To get your account balance, you need to call the nicPublicInfo operation. This operation is located in the nic service class named OvhServiceNic. When calling this operation, you'll get your nic public info.

You can also read the official Ovh NicPublicInfo SOAP operation documentation.

First step: load classes and set configuration

The generated package includes an autoload file which makes easy to load all the generated classes at once.

require_once __DIR__ . '/OvhAutoload.php';

We suppose your file is located in the root directory of the extracted package you just downloaded.

When all classes are loaded, you can define the configuration to call the SOAP API. This configuration is required to instantiate any Ovh service class.

$wsdl = array(); // you can get the last WSDL version directly from their SOAP API dedicated website: http://www.ovh.com/soapi/en/ $wsdl[OvhWsdlClass::WSDL_URL] = 'http://www.ovh.com/soapi/soapi-re-1.58.wsdl'; // no cache so you always get the latest version, slower so you can comment this line if you prefer $wsdl[OvhWsdlClass::WSDL_CACHE_WSDL] = WSDL_CACHE_NONE;

Second step: open a session

All of the OVH SOAP API operations require that you open a session and send the session id in each request. So, to open a session, you must call the login operation located in the OvhServiceLogin class. In order to open a session, do at least as below:

$login = new OvhServiceLogin($wsdl); if($login->login(new OvhStructLogin('Your OVH nic','You OVH password')) $sessionId = $login->getResult()->getReturn(); else echo 'The login has failed!';

Normally, you should have the $sessionId variable defined with a string, such as a token. To avoid calling again and agin this method, you can put the $sessionId value in session. So you'll only open a session once and use it each time you need to call a new operation.

You must know that the login operation accepts two other parameters:

  • language: your language
  • multisession: allows to open multi session

To be honest, I often not use these two parameters in my applications.

Tip:if you activated the OVH notifications yo be notified that a user logged in with your account, you should then receive an email each first time you open a session.

Last step: get your nic public info

The final step consists in calling the OvhServiceNic::nicPublicInfo() method. In order to do that, we proceed as following:

$nic = new OvhServiceNic($wsdl); if($nic->nicPublicinfo(new OvhStructNicPublicInfo($sessionId,'The OVH nic to get public info from'))) { echo 'The nic public info: nic: ' . $nic->getResult()->getReturn()->getNic() . ', firstname: ' . $nic->getResult()->getReturn()->getFirstName . ', name:' . $nic->getResult()->getReturn()->getName() . ', organisation:' . $nic->getResult()->getReturn()->getOrganisation() . ', city: ' . $nic->getResult()->getReturn()->getCity(); } else echo 'Nic public info failed, please read the error returned: ' . print_r($nic->getLastError(),true);

If everything works fine, the $nic->getResult() returns a OvhStructNicPublicInfoReturn object which contains the nic public info.

Otherwise, you have to analyze the last error catched when the OvhServiceNic::nicPublicInfo() method has been called by calling the generic OvhWsdlClass::getLastError() method.

Conclusion

As you can see, getting a nic public info is pretty trivial and quick. We saw that each value is OOP and that responses are entirely returned as objects so we can easily manipulate them. The generated classes aim to ease the communication with SOAP Web services at each step. If you have any question, feel free to send me comments on this topic. Now, you can easily call any other Ovh API operation by following this methodology.

Edit: Now you can do it using the soap client web interface.