我使用了以XML
格式返回结果的Web服务。方法simplexml_load_string()
用于将其转换为如下对象:
$xml = simplexml_load_string($result[$webresult]);
以下是使用的示例显示
print_r($xml)
:
SimpleXMLElement Object ( [record] => SimpleXMLElement Object ( [txnref] => 6113229211825720 [channel] => mastercard [amount] => 3000.00 [payment_date] => 8/17/2013 9:18:25 PM [payment_status] => successful [field_values] => SimpleXMLElement Object ( [field_values] => SimpleXMLElement Object ( [field] => Array ( [0] => SimpleXMLElement Object ( [names] => Frank [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) [1] => SimpleXMLElement Object ( [amount] => 3000 [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) [2] => SimpleXMLElement Object ( [currency] => NGN [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) [3] => SimpleXMLElement Object ( [email_address] => chibuzo.henry@gmail.com [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) [4] => SimpleXMLElement Object ( [phone_number] => 08035653468 [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) [5] => SimpleXMLElement Object ( [merch_txnref] => 134 [acct_desc] => False [acct_desc_order] => 0 [hidden] => False [defaultvalue] => SimpleXMLElement Object ( ) [xpath_field] => 0 ) ) ) ) [payment_status_description] => Transaction Successful - Approved ) )
但是,当我尝试以这种方式访问对象$xml
时:
$xml->amount;
它不会返回任何结果。如何正确访问对象的属性?
答案 0 :(得分:2)
amount属性位于记录对象中,因此请尝试$xml->record->amount;
答案 1 :(得分:1)
金额在Record对象中,因此请使用$xml->record->amount;
首先打印一个Web服务响应,一个必要的步骤记在脑海中
使用echo "<pre>";
然后您打印array
例如:
echo "<pre>";
print_r($xml);
然后只有你清楚地看到数组格式