tags:

views:

58

answers:

2
+1  Q: 

Read XML with PHP

I am trying to check a field in some XML that is returned from an outside. The XML is returned in a variable call $out and when you view the source of the page you get a XML output of the following,

<?xml version="1.0" encoding="UTF-8"?>
<ResponseBlock Live="FALSE" Version="3.51">
  <Response Type="AUTH">
    <OperationResponse>
      <TransactionReference>23-9-1334895</TransactionReference>
      <TransactionCompletedTimestamp>2010-04-30 15:59:05</TransactionCompletedTimestamp>
      <AuthCode>AUTH CODE:TEST</AuthCode>

      <TransactionVerifier>AlaUOS1MOnN/iwc5s2WPDm5ggrCLwesUnHs9h+W0N3CRaln2W6lh+6dtaRFFhLdwfnw6y7lRemyJUYl9a3dpWfzORE6DaZkFMb+dIb0Ne1UxjFEJkrEtjzx/i8KSayrIBrT/yGZOoOT42EZ9loc+UkdGk/pqYvj8bZztvgBNo2Ak=</TransactionVerifier>
      <Result>1</Result>
      <SettleStatus>0</SettleStatus>
      <SecurityResponseSecurityCode>1</SecurityResponseSecurityCode>
      <SecurityResponsePostCode>1</SecurityResponsePostCode>
      <SecurityResponseAddress>1</SecurityResponseAddress>

    </OperationResponse>
    <Order>
      <OrderInformation>This is a test order</OrderInformation>
      <OrderReference>Order0001</OrderReference>
    </Order>
  </Response>
</ResponseBlock>

I want check what value is in the 'Result' field. I am unsure how to access the information using PHP, so far I have,

$xml = simplexml_load_string($out);

Many Thanks

A: 

You can do this with an xpath query:

$xml->xpath('/ResponseBlock/Response/OperationResponse/Result');

should give you what you want.

http://www.php.net/manual/en/simplexmlelement.xpath.php

Joshua Smith
+3  A: 

Using this should be just enough :

echo $xml->Response->OperationResponse->Result;

Here, it displays :

1


SimpleXML load the XML data into a structure made of arrays and objects -- which makes it easy to access.

To know what this structure looks like, you can use :

var_dump($xml);


Which would give you, here :

object(SimpleXMLElement)[1]
  public '@attributes' => 
    array
      'Live' => string 'FALSE' (length=5)
      'Version' => string '3.51' (length=4)
  public 'Response' => 
    object(SimpleXMLElement)[2]
      public '@attributes' => 
        array
          'Type' => string 'AUTH' (length=4)
      public 'OperationResponse' => 
        object(SimpleXMLElement)[3]
          public 'TransactionReference' => string '23-9-1334895' (length=12)
          public 'TransactionCompletedTimestamp' => string '2010-04-30 15:59:05' (length=19)
          public 'AuthCode' => string 'AUTH CODE:TEST' (length=14)
          public 'TransactionVerifier' => string 'AlaUOS1MOnN/iwc5s2WPDm5ggrCLwesUnHs9h+W0N3CRaln2W6lh+6dtaRFFhLdwfnw6y7lRemyJUYl9a3dpWfzORE6DaZkFMb+dIb0Ne1UxjFEJkrEtjzx/i8KSayrIBrT/yGZOoOT42EZ9loc+UkdGk/pqYvj8bZztvgBNo2Ak=' (length=173)
          public 'Result' => string '1' (length=1)
          public 'SettleStatus' => string '0' (length=1)
          public 'SecurityResponseSecurityCode' => string '1' (length=1)
          public 'SecurityResponsePostCode' => string '1' (length=1)
          public 'SecurityResponseAddress' => string '1' (length=1)
      public 'Order' => 
        object(SimpleXMLElement)[4]
          public 'OrderInformation' => string 'This is a test order' (length=20)
          public 'OrderReference' => string 'Order0001' (length=9)


Knowing that structure, reaching the element that interests you should be quite easy ;-)

Pascal MARTIN