使用php Shopify webhook xml文件数组

时间:2014-06-03 19:36:56

标签: php xml tags shopify

我正在使用shopify webhook来更新ms sql server当订单满了时belos是我从shopify webhook获得的代码和xml文件但在order.xml文件中有两个关键标签" line-item&#34 ;一个在"顺序"标签和另一个是" fullfillments"标签所以我从order.xml文件中获得了重复的值。 我如何从" line-item"中获得一个值?标签,请帮帮我。 非常感谢你。

php代码:

$itemList2 = $dom->getElementsByTagName('line-item');


foreach($itemList2 as $item) {

$qty='';
$sku='';
$lineitemprice='';
$linetax='';

foreach($item->childNodes as $child) {

if ($child->localName == 'quantity') {
$qty = $child->textContent;
}
if ($child->localName == 'sku') {
$sku = $child->textContent;
}
if ($child->localName == 'price') {
$lineitemprice = $child->textContent;
}
if ($child->localName == 'linetax') {
$linetax = $child->textContent;
}
}   

和shopify webhook中的order.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<order>

<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service><fulfillment-status>fulfilled</fulfillment-status><gift-card type="boolean">false</gift-card><grams type="integer">0</grams><id type="integer">470618337</id><price type="decimal">0.99</price><product-id type="integer">103461847</product-id><quantity type="integer">5</quantity><requires-shipping type="boolean">true</requires-shipping><sku>695019010135</sku><taxable type="boolean">true</taxable><title>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</title><variant-id type="integer">238666967</variant-id><variant-title></variant-title><vendor>Beauty Town</vendor><name>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</name><variant-inventory-management>shopify</variant-inventory-management><properties type="array"></properties><product-exists type="boolean">true</product-exists><fulfillable-quantity type="integer">0</fulfillable-quantity><tax-lines type="array"/>
</line-item>
</line-items>


<fulfillments type="array">
<fulfillment>
<created-at type="dateTime">2014-06-03T12:15:27-04:00</created-at><id type="integer">184397429</id><order-id type="integer">265290017</order-id><service>manual</service><status>success</status><tracking-company>Other</tracking-company><updated-at type="dateTime">2014-06-03T12:15:27-04:00</updated-at><tracking-number>123456789</tracking-number><tracking-numbers type="array"><tracking-number>123456789</tracking-number></tracking-numbers><tracking-url>http://www.google.com/search?q=123456789</tracking-url><tracking-urls type="array"><tracking-url>http://www.google.com/search?q=123456789</tracking-url></tracking-urls><receipt></receipt><line-items type="array">

<line-item>
<fulfillment-service>manual</fulfillment-service><fulfillment-status>fulfilled</fulfillment-status><gift-card type="boolean">false</gift-card><grams type="integer">0</grams><id type="integer">470618337</id><price type="decimal">0.99</price><product-id type="integer">103461847</product-id><quantity type="integer">5</quantity><requires-shipping type="boolean">true</requires-shipping><sku>695019010135</sku><taxable type="boolean">true</taxable><title>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</title><variant-id type="integer">238666967</variant-id><variant-title></variant-title><vendor>Beauty Town</vendor><name>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</name><variant-inventory-management>shopify</variant-inventory-management><properties type="array"></properties><product-exists type="boolean">true</product-exists><fulfillable-quantity type="integer">0</fulfillable-quantity><tax-lines type="array"/></line-item></line-items>

</fulfillment>
</fulfillments>
</order>

1 个答案:

答案 0 :(得分:1)

您可以使用XPath,例如:

$xpath = new DOMXpath($dom);
$itemList2 = $xpath->query("/order/line-items/line-item"); // absolute path

使用这样的相对路径也应该有效:

$itemList2 = $xpath->query("line-items/*");