我正在努力通过web服务连接到另一家公司使用PHP。根据来自web服务的响应,连接成功。在此过程中没有用户验证或密码。
方法的验证也是成功的,基于print_r($client->__getFunctions())和print_r($client->__getTypes())的结果,我可以看到这两种情况的信息。
当参数传递给方法时,问题就出现了,请求在XML主体中没有参数:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:GetToken/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我做了不同类型的调用,得到相同的结果:
$response = $client->__soapCall('GetToken', array('parameters' => $params));
或
$response = $client->GetToken(array('parameters' => $params));
或
$response = $client->__soapCall('GetToken', array($params));
或
$response = $client->GetToken(array($params));
每次尝试都是来自web服务的相同响应,因为该方法没有将参数附加到请求
[GetTokenResult] => stdClass Object
(
[Token] =>
[Url] =>
[StatusCode] => 400
[StatusDescription] => Value cannot be null.
)
这是我的代码:
Try{
$wsdlLocation = 'http://webservice.com/Agent.svc?wsdl';
$client = new SoapClient($wsdlLocation,array('location' => $wsdlLocation,
'trace' => true,
'exceptions' => true
)
);
$params = array(
'Key' => '123456',
'ReturnUrl' => 'http://Anyweb.com',
'Name' => 'John Doe',
'Office' => 'NorhWest',
'Business' => 'PL',
'Email' => 'my@email.com',
);
$response = $client->__soapCall('GetToken', array('parameters' => $params));
print_r($response);
echo "REQUEST:'n" . $client->__getLastRequest() . "'n";
}
catch (SoapFault $fault) {
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
我还使用XMLspy测试了web服务。使用相同的值和参数从该工具发送请求。在这种情况下,响应是成功的。所以问题不在于其他公司的服务。
知道如何解决这个问题吗?谢谢。
好了,过了一段时间我找到了这个问题的答案。soap服务正在等待一个名为"request"而不是"parameter"的数组。这解决了所有问题。
$response = $client->__soapCall('GetToken', array('request' => $params));
我希望这能为别人节省时间。