【问题标题】:cannot read web service response in php无法读取 php 中的 Web 服务响应
【发布时间】:2015-07-18 08:45:34
【问题描述】:

我正在使用 PHP 调用 Web 服务,但在处理响应时遇到了困难。

如果我对响应进行 var_dump,这是 Web 服务返回的消息:

     stdClass Object
(
    [webserviceaction] => stdClass Object
        (
            [any] => 

<result xmlns="" corpname="test"><user id="tester">
<inbox><actionitems><title>View transcript</title>
<actionitemurl>../MainView.aspx?tab_page_id=-8&amp;Reset=TRUE</actionitemurl>
</actionitems><actionitems>
.....
</result>

试图将公司名读取为

$getUser->Webserviceaction->Result['corpName']; 

它显示 NULL。不确定如何读取对象内部的 xml。

尝试根据http://board.phpbuilder.com/showthread.php?10370637-Cannot-read-web-service-response获取最后的响应

仍然无法获取值。 这是代码。

echo "RESPONSE:\n" . $client->__getLastResponse() . "\n";
$xmlstring = $getUser->webserviceaction->any;

$xml = new SimpleXMLElement($xmlstring);


foreach ($xml->result->user->inbox->actionitems as $res)
    echo "Title" . $res->title . "\n";

请指教。

这是我打印 $xml 时看到的内容

 resultobject(SimpleXMLElement)#8 (2) {
  ["@attributes"]=>
  array(1) {
    ["corpName"]=>
    string(4) "test"
  }
  ["User"]=>
  object(SimpleXMLElement)#9 (4) {
    ["@attributes"]=>
    array(1) {
      ["id"]=>
      string(6) "tester"
    }
    ["Inbox"]=>
    object(SimpleXMLElement)#10 (1) {
      ["ActionItems"]=>
      array(2) {
        [0]=>
        object(SimpleXMLElement)#13 (2) {
          ["Title"]=>
          string(15) "View transcript"
          ["ActionItemUrl"]=>
          string(46) "../MainView.aspx?tab_page_id=-8&amp;Reset=TRUE"
        }
        [1]=>
        object(SimpleXMLElement)#14 (2) {
          ["Title"]=>
          string(32) "View Training Exemption Requests"
          ["ActionItemUrl"]=>
          string(40) "../PendingExemptionRequests.aspx?wp=1"


...

【问题讨论】:

    标签: php xml web-services soap-client


    【解决方案1】:

    我认为应该是:

    $xmlstring = $getUser->webserviceaction->any;
    

    那么循环应该是:

    foreach ($xml->User->ActionItems as $res) {
        echo "Title: " . $res->Title . "\n";
    }
    

    简单的 XML 是 case-sensitive.。并且没有result元素,这是顶级XML对象的标签。

    【讨论】:

    • 我可以使用上面的代码将 xml 代码转换为 $xmlstring,但仍然无法读取操作项中的公司名称或标题。
    • 更新了答案。
    • 感谢 Barmar 的回复。但是,这不起作用。我相信行动项目是一个列表。如果我错了,请纠正我。回声 $xml->result['corpname'] 。 "\n";获取公司名称也不起作用。
    • 它看起来不像您发布的 XML 中的列表。 &lt;actionitems&gt; 中不应该有单独的 &lt;actionitem&gt; 元素来分组吗?
    • 没关系,我看到你后面还有一个 &lt;actionitems&gt; 元素。
    猜你喜欢
    • 2012-03-30
    • 2015-11-26
    • 2021-05-26
    • 1970-01-01
    • 2015-02-09
    • 2012-12-07
    • 2020-04-09
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多