xml解析器使用php表单shopify webhook

时间:2012-11-24 04:42:10

标签: php xml parsing shopify

我在创建订单时使用webhook更新我的MS sql服务器,下面是我从shopify webhook获取的xml

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <buyer-accepts-marketing type="boolean">true</buyer-accepts-marketing>
  <closed-at type="datetime" nil="true"></closed-at>
  <currency>USD</currency>
  <email>jon@doe.ca</email>
  <financial-status>voided</financial-status>
  <fulfillment-status>pending</fulfillment-status>
  <gateway>bogus</gateway>
  <id type="integer">123456</id>
  <name>#9999</name>
  <note nil="true"></note>
  <number type="integer">234</number>
  <subtotal-price type="decimal">229.94</subtotal-price>
  <taxes-included type="boolean">false</taxes-included>
  <total-discounts type="decimal">0.0</total-discounts>
  <total-line-items-price type="decimal">229.94</total-line-items-price>
  <total-price type="decimal">239.94</total-price>
  <total-price-usd type="decimal" nil="true"></total-price-usd>
  <total-tax type="decimal">0.0</total-tax>
  <total-weight type="integer">0</total-weight>
  <updated-at type="datetime">2012-11-23T12:34:25-05:00</updated-at>
  <created-at type="datetime">2012-11-23T12:34:25-05:00</created-at>
  <token nil="true"></token>
  <landing-site nil="true"></landing-site>
  <referring-site nil="true"></referring-site>
  <cancelled-at type="datetime">2012-11-23T12:34:25-05:00</cancelled-at>
  <cancel-reason>customer</cancel-reason>
  <cart-token nil="true"></cart-token>
  <browser-ip nil="true"></browser-ip>
  <landing-site-ref nil="true"></landing-site-ref>
  <order-number type="integer">1234</order-number>
  <discount-codes type="array"/>
  <note-attributes type="array">
  </note-attributes>
  <processing-method nil="true"></processing-method>
  <line-items type="array">
    <line-item>
      <requires-shipping type="boolean">true</requires-shipping>
      <fulfillment-service>manual</fulfillment-service>
      <grams type="integer">5000</grams>
      <price type="decimal">199.99</price>
      <quantity type="integer">1</quantity>
      <sku>SKU2006-001</sku>
      <title>Sledgehammer</title>
      <product-id type="integer" nil="true"></product-id>
      <variant-id type="integer" nil="true"></variant-id>
      <vendor nil="true"></vendor>
      <variant-title nil="true"></variant-title>
      <fulfillment-status nil="true"></fulfillment-status>
      <name>Sledgehammer</name>
      <variant-inventory-management nil="true"></variant-inventory-management>
      <properties type="array">
      </properties>
    </line-item>
    <line-item>
      <requires-shipping type="boolean">true</requires-shipping>
      <fulfillment-service>manual</fulfillment-service>
      <grams type="integer">500</grams>
      <price type="decimal">29.95</price>
      <quantity type="integer">1</quantity>
      <sku>SKU2006-020</sku>
      <title>Wire Cutter</title>
      <product-id type="integer" nil="true"></product-id>
      <variant-id type="integer" nil="true"></variant-id>
      <vendor nil="true"></vendor>
      <variant-title nil="true"></variant-title>
      <fulfillment-status nil="true"></fulfillment-status>
      <name>Wire Cutter</name>
      <variant-inventory-management nil="true"></variant-inventory-management>
      <properties type="array">
      </properties>
    </line-item>
  </line-items>
  <shipping-lines type="array">
    <shipping-line>
      <title>Generic Shipping</title>
      <code nil="true"></code>
      <price type="decimal">10.0</price>
      <source>shopify</source>
    </shipping-line>
  </shipping-lines>
  <tax-lines type="array"/>
  <billing-address>
    <first-name>Bob</first-name>
    <last-name>Biller</last-name>
    <address1>123 Billing Street</address1>
    <address2 nil="true"></address2>
    <city>Billtown</city>
    <company>My Company</company>
    <country>United States</country>
    <phone>555-555-BILL</phone>
    <province>Kentucky</province>
    <zip>K2P0B0</zip>
    <latitude type="decimal" nil="true"></latitude>
    <longitude type="decimal" nil="true"></longitude>
    <name>Bob Biller</name>
    <country-code>US</country-code>
    <province-code>KY</province-code>
  </billing-address>
  <shipping-address>
    <first-name>Steve</first-name>
    <last-name>Shipper</last-name>
    <address1>123 Shipping Street</address1>
    <address2 nil="true"></address2>
    <city>Shippington</city>
    <company>Shipping Company</company>
    <country>United States</country>
    <phone>555-555-SHIP</phone>
    <province>Kentucky</province>
    <zip>K2P0S0</zip>
    <latitude type="decimal" nil="true"></latitude>
    <longitude type="decimal" nil="true"></longitude>
    <name>Steve Shipper</name>
    <country-code>US</country-code>
    <province-code>KY</province-code>
  </shipping-address>
  <fulfillments type="array"/>
  <customer>
    <email>john@test.com</email>
    <accepts-marketing type="boolean" nil="true"></accepts-marketing>
    <first-name>John</first-name>
    <last-name>Smith</last-name>
    <orders-count type="integer">0</orders-count>
    <total-spent type="decimal">0.0</total-spent>
    <note nil="true"></note>
    <created-at type="datetime" nil="true"></created-at>
    <updated-at type="datetime" nil="true"></updated-at>
    <state>disabled</state>
    <last-order-id type="integer" nil="true"></last-order-id>
    <tags></tags>
    <last-order-name nil="true"></last-order-name>
  </customer>
</order>

以下是我用来获取数据的PHP脚本(XML解析器)

$xmlString = file_get_contents('php://input');
$dom = new DomDocument();
$dom->loadXML($xmlString);
$itemList = $dom->getElementsByTagName('line-item');

foreach($itemList as $item) {

$qty='';
$sku='';

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

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

因此你可以看到我可以使用“line-item”中的数据,例如“quantity”,“sku”节点,但是我如何在“order”中使用数据,我的意思是“总价”节点

抱歉让我感到困惑,如果我做了

提前非常感谢你......

2 个答案:

答案 0 :(得分:1)

从您的代码的第一眼看,您可以通过

访问总价格节点
$order = $dom->getElementsByTagName('order');

foreach($order as $get){

    foreach($get->childNodes as $child) {
        if ($child->localName == 'total-price') {
        var_dump($child->textContent); // this will get your total-price :)
        }
    }

顺便说一句..如果你要从你的订单节点开始,那就更好.. :)我希望我回答你的问题。我在本地测试它,所以它应该工作。

更新:

从上面的帖子中轻松了解。

$order = $dom->getElementsByTagName('order');

foreach($order as $get){
    $total = $get->getElementsByTagName( "total-price" );
    var_dump($total->item(0)->nodeValue);
}

答案 1 :(得分:0)

以下是您需要制作两个文件的问题的步骤。

1)itune API的包装文件,如“tune.php”

2)文件形式重复结果“get_result_from_api.php”

第1步:获取Api网址

确保你的itune api正常工作

例如

在这里输入代码

$authentic_api = $this->endpointurl . '?api_key=' . $this->api_key . '&method=' . 'aj.jobs.search' . '&keywords=' . $this->keywords . '&location=' . $this->location. '&perpage=' . $this->perpage ;

echo $authentic_api;

OUTPUT:

http://www.authenticjobs.com/api/?api_key=3fb9a030d223ac8bb06eb07fea846182&method=aj.jobs.search&keywords=&location=&perpage=100

在新标签中检查输出api,并确保它以XML格式或HTML格式显示结果

步骤2:将api结果以XML或HTML格式存储为反...

e.g。

$xmlstr = @file_get_contents( $authentic_api );

if( !$xmlstr == null )

$xml = new SimpleXMLElement( $xmlstr );

if( empty($xml) || $xml == null )

return null;

$this->aj_results = $xml;

return $xml;

步骤3:解析xml或html结果解析结果以获取所选数据。并将其存储在数组中并将值返回“get_result_from_api.php”

第4步:使用foreach循环打印出来,你需要打印出从包装类中得到的结果。

**e.g for your xml results**

foreach($results as $result)
      {
        echo 'Currency : '.$result['currency'].'<br />';
        echo 'Email : '.$result['email'].'<br />';
        echo 'financial-status : '.$result['financial-status'].'<br />'; 
        //And all the other elements by it's tag name..
      } 

希望这件事可以帮助你。

因为“希望是好事,所以事情是最好的,没有好事会死!”