Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do SoapClient on php

Tags:

php

soap

I'm new in soapclient, I have tried to do some study online and also tried coding on soap, but seem this is still not working to me, just wandering anyone here can point out and perhaps give me some example how can I actually use the soapclint to get the feedback from the following web server?

POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/CelsiusToFahrenheit"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CelsiusToFahrenheit xmlns="http://tempuri.org/">
      <Celsius>string</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
      <CelsiusToFahrenheitResult>string</CelsiusToFahrenheitResult>
    </CelsiusToFahrenheitResponse>
  </soap:Body>
</soap:Envelope>



<?php
$url = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
$client = new SoapClient($url);


?>

What should I do for the next steps so that I can get the respond ??

like image 359
Jin Yong Avatar asked Apr 08 '10 05:04

Jin Yong


1 Answers

You first have to instanciate the SoapClient class, like you did :

$url = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
$client = new SoapClient($url);


Then, you have to call the method you want to use -- the methods names can be found in the WSDL.

For instance, we could call a method called CelsiusToFahrenheit, in this WebService :

$result = $client->CelsiusToFahrenheit( /* PARAMETERS HERE */ );


Now, the problem is to know which paramaters should be passed ; and how...

If you look at the WSDL, you'll see this portion :

<s:element name="CelsiusToFahrenheit">
  <s:complexType>
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" name="Celsius" type="s:string" />
    </s:sequence>
  </s:complexType>
</s:element>

Which indicates that this methods should be passed an array, containing 1 item, which would have "Celsius" as key, and the value to convert as value.

Which means you'd have to use this portion of PHP code :

$result = $client->CelsiusToFahrenheit(array('Celsius' => '10'));


Executing this call, and dumping the result :

var_dump($result);

Gets out this kind of output :

object(stdClass)#2 (1) {
  ["CelsiusToFahrenheitResult"]=>
  string(2) "50"
}


Which means you have to use this :

echo $result->CelsiusToFahrenheitResult . "\n";

To get the resulting value :

50


Note : the structure of this result can be found in the WSDL file too, of course -- see the CelsiusToFahrenheitResponse portion.

like image 125
Pascal MARTIN Avatar answered Oct 08 '22 14:10

Pascal MARTIN