如何使用PHP从SOAP Web服务访问返回的对象(How to access returned object from SOAP webservice using PHP)

编程入门 行业动态 更新时间:2024-10-20 04:03:27
如何使用PHP从SOAP Web服务访问返回的对象(How to access returned object from SOAP webservice using PHP)

我正在尝试使用基于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 property

However 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->any

You can load string with DomDocument or simplexml lib.

更多推荐

本文发布于:2023-07-16 12:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   对象   SOAP   PHP   webservice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!