0

我使用了一个以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 ) )

但是,当我尝试访问 object$xml时,这样:

$xml->amount;

它不返回任何结果。如何正确访问对象的属性?

4

2 回答 2

2

amount 属性位于记录对象中,因此请尝试$xml->record->amount;

于 2013-08-20T23:06:16.543 回答
1

金额在 Record 对象中,所以使用$xml->record->amount;

首先,您打印一个 Web 服务响应,一个必要的步骤请记住

利用echo "<pre>";

然后你打印你的array

例如:

echo "<pre>";
print_r($xml);

那么只有你看清楚数组格式

于 2013-08-21T05:45:47.357 回答