1

我正在研究支付解决方案,需要一些 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;

但显然有什么问题,因为它不会回应任何东西.. 建议?

4

5 回答 5

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 树可作为对象使用——XML 的根节点不存在于树中,因为它是根节点;这就是为什么我不必包含payer来访问数据。

于 2010-03-02T21:36:44.087 回答
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);
于 2010-03-02T21:35:51.087 回答
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
}
于 2010-03-02T21:39:41.530 回答
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;
于 2010-03-02T21:40:15.037 回答
0

这是使用 DOM 的解决方案

第一个建议:设置error_reporting(-1)以查看任何潜在的错误。如果你这样做,你会看到 PHP 提出几个关于

Notice: Trying to get property of non-object

用于->item(0)调用 DOMNodeList 元素。相当简单的解决方案是使用

$orderXML->loadXml($order);

而不是load(), 因为load从文件加载 XML 文档。

于 2010-03-02T21:57:58.227 回答