我正在尝试使用基于SOAP的webservice uisng PHP。 以下是示例代码。 我需要知道/学习如何访问返回的对象元素? 请注意我是PHP的新手
$url = 'http://www.webservicex.net/uszip.asmx?WSDL'; $soap = new SoapClient($url, array( "trace" => 1, // enable trace to view what is happening "exceptions" => 0, // disable exceptions "cache_wsdl" => 0) ); try { $result = $soap->GetInfoByZIP(array('USZip' => '97219')); echo($result->$CITY); //print_r( $soap->GetInfoByZIP(array("USZip" => "97219"))); } catch (SoapFault $e) { echo "Error: {$e->faultstring}"; }我得到以下异常
Notice: Undefined variable: CITY Fatal error: Cannot access empty property但是,当我执行上面的注释行时,它返回以下响应
stdClass Object ( [GetInfoByZIPResult] => stdClass Object ( [any] => <NewDataSet xmlns=""><Table><CITY>Portland</CITY><STATE>OR</STATE><ZIP>97219</ZIP><AREA_CODE>503</AREA_CODE><TIME_ZONE>P</TIME_ZONE></Table></NewDataSet> ) )所以这意味着正在返回数据,但我无法像在.NET中那样访问它
任何人都可以帮助我如何在PHP中访问此对象,为什么?
I am trying to consume a SOAP based webservice uisng PHP. Following is the sample code. I need to know/learn how to access the retur'ed object elements? Please note i am new to PHP
$url = 'http://www.webservicex.net/uszip.asmx?WSDL'; $soap = new SoapClient($url, array( "trace" => 1, // enable trace to view what is happening "exceptions" => 0, // disable exceptions "cache_wsdl" => 0) ); try { $result = $soap->GetInfoByZIP(array('USZip' => '97219')); echo($result->$CITY); //print_r( $soap->GetInfoByZIP(array("USZip" => "97219"))); } catch (SoapFault $e) { echo "Error: {$e->faultstring}"; }I get the following exception
Notice: Undefined variable: CITY Fatal error: Cannot access empty propertyHowever when I execute the commented line above it return the following response
stdClass Object ( [GetInfoByZIPResult] => stdClass Object ( [any] => <NewDataSet xmlns=""><Table><CITY>Portland</CITY><STATE>OR</STATE><ZIP>97219</ZIP><AREA_CODE>503</AREA_CODE><TIME_ZONE>P</TIME_ZONE></Table></NewDataSet> ) )So this means that data is being returned but i am not able to access it like the way done in .NET
Can anyone please help me how to access this object in PHP and why?
最满意答案
首先,你使用$ CITY变量来访问$ result属性,你还没有定义它。
因此,如果你想在“结果”对象中获得“CITY”属性,你应该通过“$ result-> City”来实现。
根据你得到的结果 - 它是一个xml字符串,而不是对象。 如果你想访问字符串,请这样做:
$result->GetInfoByZIPResult->any您可以使用DomDocument或simplexml lib加载字符串。
First of all, u're using $CITY variable to access $result property and you haven't defined it yet.
So if you want to get "CITY" property inside "result" object you should do it by "$result->City".
According to result you get - it's a xml string, not object. If you want to access string do it this way:
$result->GetInfoByZIPResult->anyYou can load string with DomDocument or simplexml lib.
更多推荐
发布评论