我正在进行支付,需要一些PHP帮助。我在做HTTPRequest,作为回应,我会得到一些XML。 XML可能如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<payer>
<purchase_list>
<freeform_purchase>
<line_number>1</line_number>
<description>description</description>
<price_including_vat>12</price_including_vat>
<vat_percentage>
15
</vat_percentage>
<quantity>10</quantity>
</freeform_purchase>
</purchase_list>
<payment_urls>
<auth_url>authurl</auth_url>
<settle_url>settleurl</settle_url>
</payment_urls>
</payer>
基本上我想要做的是从标签中获取内容并将其保存在字符串中。
我试过了:
$order = '<?xml version="1.0" encoding="utf-8" ?>
<payer>
<purchase_list>
<freeform_purchase>
<line_number>1</line_number>
<description>description</description>
<price_including_vat>12</price_including_vat>
<vat_percentage>
15
</vat_percentage>
<quantity>10</quantity>
</freeform_purchase>
</purchase_list>
<payment_urls>
<auth_url>authurl</auth_url>
<settle_url>settleurl</settle_url>
</payment_urls>
</payer>';
$orderXML = new DOMDocument();
$orderXML->load($order);
$payerXML = $orderXML->getElementsByTagName( 'payer' );
$purchase_listXML = $orderXML->getElementsByTagName( 'purchase_list' );
$freeform_purchaseXML = $orderXML->getElementsByTagName( 'freeform_purchase' );
$linenumberXML = $orderXML->getElementsByTagName( 'line_number' );
$descriptionXML = $orderXML->getElementsByTagName( 'description' );
$price_inc_vatXML = $orderXML->getElementsByTagName( 'price_including_vat' );
$vat_percentageXML = $orderXML->getElementsByTagName( 'vat_percentage' );
$quantityXML = $orderXML->getElementsByTagName( 'quantity' );
$settle_urlXML = $orderXML->getElementsByTagName( 'settle_url' );
$auth_urlXML = $orderXML->getElementsByTagName( 'auth_url' );
$theLineNumber = $linenumberXML->item(0)->nodeValue;
$theValue = $descriptionXML->item(0)->nodeValue;
$freeform_price = $price_inc_vatXML->item(0)->nodeValue;
$freeform_vat = $vat_percentageXML->item(0)->nodeValue;
$freeform_quantity = $quantityXML->item(0)->nodeValue;
$Settle_url = $settle_urlXML->item(0)->nodeValue;
$Auth_url = $auth_urlXML->item(0)->nodeValue;
echo 'thLineNumber - ' . $theLineNumber;
echo $theValue;
echo $freeform_price;
echo $freeform_vat;
echo $freeform_quantity;
echo $Settle_url;
echo $Auth_url;
但显然有一些错误,因为它不会回应任何东西......建议?
答案 0 :(得分:4)
如果您只是尝试读取 XML字符串中的某些数据,最简单的方法可能是使用SimpleXML,而不是DOM - DOM很好适用于编写XML,但对于阅读,SimpleXML很容易使用。
例如,你可以使用这样的东西作为起点:
注意我使用simplexml_load_string
函数加载XML字符串。
$string = <<<STR
<?xml version="1.0" encoding="utf-8" ?>
<payer>
<purchase_list>
<freeform_purchase>
<line_number>1</line_number>
<description>description</description>
<price_including_vat>12</price_including_vat>
<vat_percentage>
15
</vat_percentage>
<quantity>10</quantity>
</freeform_purchase>
</purchase_list>
<payment_urls>
<auth_url>authurl</auth_url>
<settle_url>settleurl</settle_url>
</payment_urls>
</payer>
STR;
$xml = simplexml_load_string($string);
echo intval($xml->purchase_list->freeform_purchase->price_including_vat) . '<br />';
echo (string)$xml->payment_urls->auth_url . '<br />';
这会给你以下输出:
12
authurl
基本上,使用SimpleXML,XML树可用作对象 - 树的根节点不存在于树中,因为它是根;这就是为什么我不必包含payer
来访问数据的原因。
答案 1 :(得分:0)
你可能会看SimpleXML - 然后使用DOMDocument要简单得多。它将以解析的对象形式返回XML数据,以便您可以执行以下操作:
$order = '<?xml version="1.0" encoding="utf-8" ?>
<payer>
<purchase_list>
<freeform_purchase>
<line_number>1</line_number>
<description>description</description>
<price_including_vat>12</price_including_vat>
<vat_percentage>
15
</vat_percentage>
<quantity>10</quantity>
</freeform_purchase>
</purchase_list>
<payment_urls>
<auth_url>authurl</auth_url>
<settle_url>settleurl</settle_url>
</payment_urls>
</payer>';
// Alternative version: $xml = new SimpleXMLElement($order);
// (completely equivalent to the below line)
$xml = simplexml_load_string($order);
$vat_pct = intval($xml->purchase_list->freeform_purchase->vat_percentage);
答案 2 :(得分:0)
尝试使用simplexml_load_string($ string); 然后就像这样做一个foreach吧
$xml = simplexml_load_string($string);
foreach($xml as $item)
{
echo $item->description; // would print the description
// continiu your logic here
}
答案 3 :(得分:0)
不要将DOM用于此类事情,而是使用SimpleXML。
$payer = simplexml_load_string($order);
$freeform_purchase = $payer->purchase_list->freeform_purchase;
echo "Line number: ", $freeform_purchase->line_number, "\n";
echo "Description: ", $freeform_purchase->description, "\n";
echo "Auth URL: ", $payer->payment_urls->auth_url, "\n";
echo "Settle URL: ", $payer->payment_urls->settle_url, "\n";
// if you use the values as strings, cast them as strings
$description = (string) $freeform_purchase->description;
// if you have to use the values for math, cast them as integers
$vat_percentage = (int) $freeform_purchase->vat_percentage;
答案 4 :(得分:0)
以下是DOM
的解决方案第一个建议:设置error_reporting(-1)
以查看任何潜在错误。如果你这样做,你会看到PHP提出几个关于
Notice: Trying to get property of non-object
用于->item(0)
对DOMNodeList元素的调用。相当简单的解决方案是使用
$orderXML->loadXml($order);
而不是load()
,因为load
从文件加载XML文档。