Novice
Novice

Reputation: 77

How to access XML response

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) "[email protected]"
      ["Phone"]=>
      string(12) "+13474707707"
      ["UserAccountId"]=>
      string(3) "602"
    }
  }
}

Upvotes: 0

Views: 66

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57141

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;
   }
}

Upvotes: 1

Related Questions