A couple of days ago i received a job asking to pull information automatically from a web service. The web service was using soap. I have used many api’s with REST but never soap. There wasn’t a great deal of documentation and what there was confused me event more.
I used PHP’s built in soapclient class. Soap needs to be enabled in your php.ini settings for the soap functions to work.
The Soapclient construct takes 2 arguments, 1) the wsdl file – a web service definition list in xml format that describes the functions and parameters to use – or Null if working in non-wsdl mode 2) an array of parameters
$client = new soapclient(
‘http://…………………./Postings.asmx?WSDL’,
array(
‘location’ => ‘http://………………………………../Postings.asmx’,
‘trace’ => 1,
‘style’ => SOAP_RPC,
‘use’ => SOAP_ENCODED
)
);
The array includes the location of the soap server, a trace set to 1 to give you an error back trace, the style and use parameters are used only in non-wsdl mode.
If you look over a sample wsdl file you will see section like so:
<s:element name=”GetNewestPostings”><s:complexType><s:sequence><s:element minOccurs=”1″ maxOccurs=”1″ name=”Language” type=”tns:Language”/><s:element minOccurs=”1″ maxOccurs=”1″ name=”Count” type=”s:int”/></s:sequence></s:complexType></s:element>
There is also a good soap tutorial of functions you can use to find out the function calls of the wsdl file and other useful information from the request and response.
The next thing to do is to call a function from the wsdl file. So from the sample above we will call the getNewestPost() function on the soapclient object. First we need to build an array of parameters corresponding to the node and type the function expects. For example:
$params = array(
‘Language’=>’en_US’,
‘Count’=> 20
);
For multiple parameter types send an array of values:
$params = array(
‘Languages’=>array(‘en_US’, ‘CA’, ‘zh-tw’),
‘Count’=> 20
);
Now run the function in a try catch statement, it will throw a soapfault exception if there was a problem:
try{
$data = $client->getNewestPost($params);print_r($data);
}
catch(SoapFault $e){
echo “SOAP Fault: “.$e->getMessage().”";
}
Also be sure to check out the soap class php.net manual if your still receiving errors contact the soap server admin, it may be they need to add your server ip to there allowed list.




