2010-04-30 78 views
1

我試圖檢查從外部返回的某些XML字段。 XML是在一個變量調用$out返回,當您查看頁面的源代碼將得到以下的XML輸出,使用PHP讀取XML

<?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> 

我要檢查什麼值在「結果」字段中。我不確定如何訪問使用PHP的信息,到目前爲止,我,

$xml = simplexml_load_string($out); 

非常感謝

回答

3

使用這應該是剛好足夠的:

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

在這裏,它顯示:

1 


SimpleXML的XML數據加載到由數組和對象的結構 - 這使得它很容易訪問。

要知道,這種結構是什麼樣子,你可以使用:

var_dump($xml); 


這將使你,在這裏:

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) 


明知結構,到達元素利益你應該很容易;-)