How to do SoapClient on php

丶灬走出姿态 提交于 2019-12-23 10:19:38

问题


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 ??


回答1:


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.



来源:https://stackoverflow.com/questions/2597718/how-to-do-soapclient-on-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!