class stdClass 的对象无法转换为 SOAP 请求的字符串
Object of class stdClass could not be converted to string for SOAP request
当我 运行 以下脚本时,出现“ class stdClass 的对象无法转换为 SOAP 请求 的字符串”错误$client->LatLonListZipCode($args)
行,我不知道为什么。有什么想法吗?
<?php
$contextOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'http' => array(
'timeout' => 5 //seconds
)
);
//create stream context
$stream_context = stream_context_create($contextOptions);
//create client instance (over HTTPS)
$client = new SoapClient('http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl', array(
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'trace' => 1,
'stream_context' => $stream_context,
'soap_version'=> SOAP_1_2,
'connection_timeout' => 5 //seconds
));//SoapClient
$args = new stdClass();
$args->zipCodeList = '10001';
$z = $client->LatLonListZipCode($args);
异常原因
首先 - 此服务使用 SOAP 1.1 不 SOAP 1.2。将您的 $client
规格更改为:
$client = new SoapClient('http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl', array(
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'trace' => 1,
'stream_context' => $stream_context,
'soap_version'=> SOAP_1_1,//<-- note change here
'connection_timeout' => 5 //seconds
));//SoapClient
服务定义
如您的WSDL service specification所述,您可以发现LatLonListZipCode
函数定义为:
<operation name="LatLonListZipCode">
<documentation>Returns a list of latitude and longitude pairs with each pair corresponding to an input zip code.</documentation>
<input message="tns:LatLonListZipCodeRequest"/>
<output message="tns:LatLonListZipCodeResponse"/>
</operation>
预期参数定义为:
<xsd:simpleType name="zipCodeListType">
<xsd:restriction base='xsd:string'>
<xsd:pattern value="\d{5}(\-\d{4})?( \d{5}(\-\d{4})?)*" />
</xsd:restriction>
</xsd:simpleType>
正确调用
所以我们知道,服务器只需要一个名为 zipCodeList
的 string
参数。现在我们可以推断出你的代码应该是这样的:
$args = array("zipCodeList"=>'10001');
try {
$z = $client->LatLonListZipCode($args);
} catch (SoapFault $e) {
echo $e->faultcode;
}
请注意,我正在捕获 SoapFault
异常。它将帮助您了解服务器端错误。在 PHP documentation.
中阅读更多相关信息
当我 运行 以下脚本时,出现“ class stdClass 的对象无法转换为 SOAP 请求 的字符串”错误$client->LatLonListZipCode($args)
行,我不知道为什么。有什么想法吗?
<?php
$contextOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'http' => array(
'timeout' => 5 //seconds
)
);
//create stream context
$stream_context = stream_context_create($contextOptions);
//create client instance (over HTTPS)
$client = new SoapClient('http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl', array(
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'trace' => 1,
'stream_context' => $stream_context,
'soap_version'=> SOAP_1_2,
'connection_timeout' => 5 //seconds
));//SoapClient
$args = new stdClass();
$args->zipCodeList = '10001';
$z = $client->LatLonListZipCode($args);
异常原因
首先 - 此服务使用 SOAP 1.1 不 SOAP 1.2。将您的 $client
规格更改为:
$client = new SoapClient('http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl', array(
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'trace' => 1,
'stream_context' => $stream_context,
'soap_version'=> SOAP_1_1,//<-- note change here
'connection_timeout' => 5 //seconds
));//SoapClient
服务定义
如您的WSDL service specification所述,您可以发现LatLonListZipCode
函数定义为:
<operation name="LatLonListZipCode">
<documentation>Returns a list of latitude and longitude pairs with each pair corresponding to an input zip code.</documentation>
<input message="tns:LatLonListZipCodeRequest"/>
<output message="tns:LatLonListZipCodeResponse"/>
</operation>
预期参数定义为:
<xsd:simpleType name="zipCodeListType">
<xsd:restriction base='xsd:string'>
<xsd:pattern value="\d{5}(\-\d{4})?( \d{5}(\-\d{4})?)*" />
</xsd:restriction>
</xsd:simpleType>
正确调用
所以我们知道,服务器只需要一个名为 zipCodeList
的 string
参数。现在我们可以推断出你的代码应该是这样的:
$args = array("zipCodeList"=>'10001');
try {
$z = $client->LatLonListZipCode($args);
} catch (SoapFault $e) {
echo $e->faultcode;
}
请注意,我正在捕获 SoapFault
异常。它将帮助您了解服务器端错误。在 PHP documentation.