douleng0794 2017-09-17 06:43 采纳率: 0%
浏览 47
已采纳

如何访问XML响应

Good day, I want to access the XML response and echo it to display its value but I don't know how to do it. I already tried some few answers in StackOverflow but I fail.

This is my code.

<?php
error_reporting(E_ALL);
require_once 'ruFunctions.php';

$rentalsUnited = new rentalsUnited();

$ru= $rentalsUnited->getOwners();

if($ru != null){
   $data= simplexml_load_string($ru);
   var_dump($data); // it will return boof(false)
   var_dump($ru);
   echo $data->Pull_ListAllOwners_RS->Status['ID']; //Trying to get property of non-object
}
?>

Results for var_dump($ru);

object(SimpleXMLElement)#2 (3) {
  ["Status"]=>
  string(7) "Success"
  ["ResponseID"]=>
  string(32) "44065d9888304e8cba912bce4d131ab1"
  ["Owners"]=>
  object(SimpleXMLElement)#3 (1) {
    ["Owner"]=>
    object(SimpleXMLElement)#4 (7) {
      ["@attributes"]=>
      array(1) {
        ["OwnerID"]=>
        string(6) "429335"
      }
      ["FirstName"]=>
      string(5) "Test"
      ["SurName"]=>
      string(7) "Tester"
      ["CompanyName"]=>
      string(15) "Test Helpers"
      ["Email"]=>
      string(23) "info@Test.com"
      ["Phone"]=>
      string(12) "+13474707707"
      ["UserAccountId"]=>
      string(3) "602"
    }
  }
}
  • 写回答

1条回答 默认 最新

  • doudao1950 2017-09-17 06:56
    关注

    It looks like $ru is already a SimpleXMLElement, so trying to call simplexml_load_string will fail on this.

    You can see some of the details by

    if($ru != null){
       echo $ru->Status;
    }
    

    You can (probably) list the owners by...

    if($ru != null){
       foreach ($ru->Owners->Owner as $owner ) {
         echo "ownerId=".$owner['OwnerID'].PHP_EOL;
         echo "FirstName=".$owner->FirstName.PHP_EOL;
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题