|
|
|
Introduction: |
SOAP support is built into PHP5 natively, so we can forgo having to download an additional package off PEAR, but
you will need to enable it as the interpreter of choice for .php files. If you haven't already, head on over to the Resource Center to learn
how to enable PHP5.
Download This Example
Fundamental Assumptions: As with all examples outlined below, we will assume a few things about
the environment:
* Your SOAP key is 12345678-12345678-12345678-12345678
* The endpoint is https://cp.assmule.apisnetworks.com/soap.php
* The WSDL is located at https://cp.assmule.apisnetworks.com/esprit.wsdl
* Authentication is done via the URL and not through HTTP Authentication
* Scalar example will be from common_get_mail_server_name
* Vector example will be from common_get_disk_quota
|
|
|
Non-WSDL Mode: |
First thing first is to initialize the SOAP client from your PHP script:
<?php
/** authkey generated from within the control panel */
$authkey = '12345678-12345678-12345678-12345678';
/** Location of the SOAP endpoint */
$endpoint = 'https://cp.assmule.apisnetworks.com/soap.php';
/** First step, initialize the SOAP client: */
/** URL Authentication Method: */
$client = new
SoapClient(NULL,
array('connection_timeout' => 5,
'location' => $endpoint.
'?authkey='.$authkey,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED,
'uri' => 'urn:net.esprit.soap'));
/** HTTP Authentication Method: */
$client = new
SoapClient(NULL,
array('connection_timeout' => 5,
'location' => $endpoint,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED,
'uri' => 'urn:net.esprit.soap',
'login' => 'soap',
'password' => $authkey));
?>
Notes: The first parameter is the WSDL. As we are working in non-WSDL mode, we supply the value NULL to inform the SOAP client there is no WSDL. This
defers style of the SOAP request to the server.
|
|
|
Handling Scalars: |
<?php
/** Scalar test: */
$mail_server = $client->common_get_mail_server_name();
print ("Mail server name: " . $mail_server);
/** Prints:
* Mail server name: mail.demosite.apisnetworks.com
*/
?>
Notes: Scalars are treated as single data variables.
|
|
|
Handling Structs: |
<?php
/** We can even reuse the same $client object: */
$diskSpaceArr = $client->common_get_disk_quota();
/** Want to dump the structure of the struct? Use var_export(): */
print (var_export($diskSpaceArr,true));
/** Prints:
* array (
* 'total' => 10240,
* 'used' => 10908,
* )
*/
/** We now have a struct in PHP5 that is represented as an array.
* Want to know the individual keys?
*/
print (var_export(array_keys($diskSpaceArr),true));
/** Prints:
* array ( 0 => 'total',
* 1 => 'used', )
*/
/** Print out disk space used and free. The following below will output:
* Disk space used: 13000
* Disk space free: 18500
*/
print ("Disk space used: ".$diskSpaceArr['used']."\n".
"Disk space free: ".($diskSpaceArr['total']-$diskSpaceArr['used'])."\n");
?>
Notes: Structs are treated just like associative arrays in the SOAP extension for PHP5. If you're ever curious about the structure, dump the variable returned from the SOAP call
via var_export(). You can even reuse the $client object created by Soap_Client::__construct().
|
|
|
Handling SOAP Faults: |
<?php
try {
$client->common_non_existent_function();
} catch (SoapFault $e) {
print ("Oops! We got an error: ".$e->faultstring );
}
/** Prints:
* Oops! We got an error: Procedure 'common_non_existent_function' not present
*/
?>
Notes: In PHP5 exception handling was added. SOAP conforms to this change and as a result will throw
an exception with a SoapFault object. Failure to catch an exception will terminate a script abruptly, like a die().
|
|