1

我在创建订单时使用 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”中的数据,我的意思是“total-price”节点

很抱歉让你感到困惑,如果我这样做了

非常感谢您提前...

4

2 回答 2

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);
}
于 2012-11-24T05:13:24.253 回答
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 格式,反之亦然。

例如

$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..
      } 

希望这些东西可以帮助你。

因为“希望是美好的事物,可能是最好的事物,美好的事物永远不会消亡!”

于 2012-11-24T05:15:50.730 回答