我是 soapclient 的新手,我尝试过在线学习,也尝试过在 soap 上编码,但似乎这对我来说仍然不起作用,只是在这里闲逛的任何人都可以指出,也许给我一些例子如何我实际上使用 soapclint 从以下 Web 服务器获取反馈?
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);
?>
接下来我应该怎么做才能得到回复??
最佳答案
您首先必须实例化SoapClient
类,就像您所做的那样:
$url = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
$client = new SoapClient($url);
然后,您必须调用您想要使用的方法——方法名称可以在 WSDL 中找到。
例如,我们可以在这个 WebService 中调用一个名为 CelsiusToFahrenheit
的方法:
$result = $client->CelsiusToFahrenheit( /* PARAMETERS HERE */ );
现在,问题是要知道应该传递哪些参数;以及如何...
如果您查看 WSDL,您会看到这部分:
<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>
这表明应该向此方法传递一个数组,其中包含 1 个项目,该项目将以“Celsius
”作为键,并将要转换的值作为值。
这意味着您必须使用这部分 PHP 代码:
$result = $client->CelsiusToFahrenheit(array('Celsius' => '10'));
执行此调用并转储结果:
var_dump($result);
得到这样的输出:
object(stdClass)#2 (1) {
["CelsiusToFahrenheitResult"]=>
string(2) "50"
}
这意味着你必须使用这个:
echo $result->CelsiusToFahrenheitResult . "\n";
获取结果值:
50
注意:这个结果的结构也可以在 WSDL 文件中找到,当然——请参阅 CelsiusToFahrenheitResponse
部分。
关于php - 如何在 php 上做 SoapClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597718/