获取文本文件的内容并将其插入XML文件中的必要位置

时间:2014-08-25 18:10:27

标签: php xml wordpress parsing amazon

我正在使用Wordpress创建一个网站 - 我正在使用Amazon MWS PHP API将Amazon FBA(亚力克的Fulfillment)服务与woocommerce插件集成。到目前为止,我已经找到了如何生成库存报告,然后使用API​​附带的PHP示例脚本下载它。现在,要将库存数据与woocommerce插件集成 - 我需要创建一个类似于随woocommerce插件提供的dummydata.xml文件的XML文件。这似乎是一个相当简单的问题,我之前编写过脚本来读取和写入文件 - 但我想确保使用正确的工具来完成工作。我下载后,库存文本文件看起来就像这样。

  

item-name item-description listing-id卖家sku价格数量open-date image-url item-is-marketplace product-id-type zshop-shipping-fee item-note item-condition zshop-category1 zshop-browse -path zshop-storefront-feature asin1 asin2 asin3 will-ship-international加速运输zshop-boldface product-id bid-for-featured-placement add-delete pending-quantity fulfillment-channel   Pioneer HDJ-1000 DJ耳机[电子] 0625OSJKQMG 3U-R7EP-QODZ 89 2014-06-25 12:26:27 PDT y 1请注意颜色为银色,如图所示。未开封的制造商包装。 11 B0002DV7Z2 B0002DV7Z2 AMAZON_NA   上帝的眼睛(西格玛力量)[大众市场平装]作者:Rollins,James 0624OQ2O5MM 86-CK3P-PHK6 4.89 0 2014-06-24 13:36:21 PDT y 1状态良好,盖子上有轻微的折痕,但非常干净。 1 0061785679 1 N 0061785679 0默认   创始兄弟:革命一代[平装]作者:Ellis,Joseph J. 0514O4WDGIG NX-USS1-UXRY 3 2014-05-14 08:09:50 PDT y 1 1 0375705244 1 N 0375705244 AMAZON_NA   JBL Flip无线蓝牙音箱(红色)[个人电脑] 0513O3A7IHY VB-GNYY-PMZS 89 0 2014-05-13 16:19:12 PDT y 1 11 B00DQAPJSY B00DQAPJSY 0 DEFAULT

试图筛选所有这些意义 - 我创建了另一个文本文件,指出所有数据是什么 - 第一个块只是所有数据所属类别的列表 - 我尽我所能将数据的每个部分分配到正确的类别,项目以第二个块开头:

  

| item-name | item-description | listing-id |卖家sku |价格|数量|开放日期| image-url | item-is-marketplace | product-id-type | zshop-shipping-fee | item-note | item-condition | zshop-category1 | zshop-browse-path | zshop-storefront-feature | asin1 | asin2 | asin3 |将船舶国际|加急运输| zshop-boldface | product-id | bid-for-featured-placement | add-delete |待定数量|履行渠道|

     

| item-name:Pioneer HDJ-1000 DJ耳机[电子] | listing-id:0625OSJKQMG | seller-sku:3U-R7EP-QODZ |价格:89 |数量:0时为空白open-date:2014-06-25 12:26:27 PDT | image_url:没有图片| item-is-marketplace:y | product-id-type:1 | zshop-shipping-fee:无运费| item-note:请注意颜色为银色,如图所示。 | item-condition:未开封的制造商包装。 | zshop-category1?:11 | asin:B0002DV7Z2 | asin:B0002DV7Z2 |频道:AMAZON_NA |

     

| item-name:上帝的眼睛(西格玛力量)[大众市场平装],罗林斯,詹姆斯| listing-id:0624OQ2O5MM | seller-sku:86-CK3P-PHK6 |价格:4.89 |数量:0(这个是因为某种原因)|开放日期:2014-06-24 13:36:21 PDT | item-is-mp:y | prod-id-type:1 |项目条件:良好的条件,盖子上的轻微折痕,但非常干净。 | zshop-category1:1 | asin:0061785679 | ?:1 | will-ship-international:N | asin:0061785679 |待定数量:0 |频道:默认(当商家)|

     

| item-name:创始兄弟:革命一代[平装],Ellis,Joseph J. | listing-id:0514O4WDGIG | seller-sku:NX-USS1-UXRY |价格:3 |日期:2014-05-14 08:09:50 PDT |是市场:y | product-id-type:1 | ?:1 | asin:0375705244 | ?:1 |国际:N | asin:0375705244 |频道:AMAZON_NA |

     

| item-name:JBL Flip无线蓝牙音箱(红色)[个人电脑] | list-id:0513O3A7IHY | sku:VB-GNYY-PMZS |价格:89 |数量:0 |日期:2014-05-13 16:19:12 PDT |是市场:y | prod-id-type:1 | zshop-category?:11 | asin:B00DQAPJSY | asin:B00DQAPJSY | ?:0 |频道:DEFAULT |

这就是dummydata.xml文件在开头看起来的样子 - 我将XML包含在一个项目中:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->

<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!--    on the site. For each author, you may choose to map to an -->
<!--    existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!--    contained in this file into your site. -->

<!-- generator="WordPress/3.5.1" created="2013-06-14 12:35" -->
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>

<channel>
    <title>WooCommerce Dummy Data</title>
    <link>http://demo2.woothemes.com/woocommerce</link>
    <description>Just another WooThemes Demo site</description>
    <pubDate>Fri, 14 Jun 2013 12:35:54 +0000</pubDate>
    <language>en-US</language>
    <wp:wxr_version>1.2</wp:wxr_version>
    <wp:base_site_url>http://demo2.woothemes.com/</wp:base_site_url>
    <wp:base_blog_url>http://demo2.woothemes.com/woocommerce</wp:base_blog_url>

    <wp:term><wp:term_id>17</wp:term_id><wp:term_taxonomy>pa_color</wp:term_taxonomy><wp:term_slug>black</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Black]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>19</wp:term_id><wp:term_taxonomy>pa_color</wp:term_taxonomy><wp:term_slug>blue</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Blue]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>12</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>cancelled</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[cancelled]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>10</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>completed</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[completed]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>5</wp:term_id><wp:term_taxonomy>product_type</wp:term_taxonomy><wp:term_slug>external</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[external]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>7</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>failed</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[failed]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>16</wp:term_id><wp:term_taxonomy>pa_color</wp:term_taxonomy><wp:term_slug>green</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Green]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>3</wp:term_id><wp:term_taxonomy>product_type</wp:term_taxonomy><wp:term_slug>grouped</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[grouped]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>8</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>on-hold</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[on-hold]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>6</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>pending</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[pending]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>9</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>processing</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[processing]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>11</wp:term_id><wp:term_taxonomy>shop_order_status</wp:term_taxonomy><wp:term_slug>refunded</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[refunded]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>2</wp:term_id><wp:term_taxonomy>product_type</wp:term_taxonomy><wp:term_slug>simple</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[simple]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>4</wp:term_id><wp:term_taxonomy>product_type</wp:term_taxonomy><wp:term_slug>variable</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[variable]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>14</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>clothing</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Clothing]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>18</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>hoodies</wp:term_slug><wp:term_parent>clothing</wp:term_parent><wp:term_name><![CDATA[Hoodies]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>13</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>music</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Music]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>20</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>posters</wp:term_slug><wp:term_parent></wp:term_parent><wp:term_name><![CDATA[Posters]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>21</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>singles</wp:term_slug><wp:term_parent>music</wp:term_parent><wp:term_name><![CDATA[Singles]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>15</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>t-shirts</wp:term_slug><wp:term_parent>clothing</wp:term_parent><wp:term_name><![CDATA[T-shirts]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>22</wp:term_id><wp:term_taxonomy>product_cat</wp:term_taxonomy><wp:term_slug>albums</wp:term_slug><wp:term_parent>music</wp:term_parent><wp:term_name><![CDATA[Albums]]></wp:term_name></wp:term>

    <generator>http://wordpress.org/?v=3.5.1</generator>

    <item>
        <title>T_1_front</title>
        <link>http://demo2.woothemes.com/woocommerce/product/woo-logo/t_1_front/</link>
        <pubDate>Fri, 07 Jun 2013 10:35:28 +0000</pubDate>
        <dc:creator>wooteam</dc:creator>
        <guid isPermaLink="false">http://demo2.woothemes.com/dummydata/files/2013/06/T_1_front.jpg</guid>
        <description></description>
        <content:encoded><![CDATA[]]></content:encoded>
        <excerpt:encoded><![CDATA[]]></excerpt:encoded>
        <wp:post_id>16</wp:post_id>
        <wp:post_date>2013-06-07 10:35:28</wp:post_date>
        <wp:post_date_gmt>2013-06-07 10:35:28</wp:post_date_gmt>
        <wp:comment_status>open</wp:comment_status>
        <wp:ping_status>open</wp:ping_status>
        <wp:post_name>t_1_front</wp:post_name>
        <wp:status>inherit</wp:status>
        <wp:post_parent>15</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type>attachment</wp:post_type>
        <wp:post_password></wp:post_password>
        <wp:is_sticky>0</wp:is_sticky>
        <wp:attachment_url>http://demo2.woothemes.com/woocommerce/files/2013/06/T_1_front.jpg</wp:attachment_url>
        <wp:postmeta>
            <wp:meta_key>_wp_attached_file</wp:meta_key>
            <wp:meta_value><![CDATA[2013/06/T_1_front.jpg]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>_wp_attachment_metadata</wp:meta_key>
            <wp:meta_value><![CDATA[a:5:{s:5:"width";i:1000;s:6:"height";i:1000;s:4:"file";s:21:"2013/06/T_1_front.jpg";s:5:"sizes";a:6:{s:9:"thumbnail";a:4:{s:4:"file";s:21:"T_1_front-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:21:"T_1_front-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:14:"post-thumbnail";a:4:{s:4:"file";s:21:"T_1_front-624x624.jpg";s:5:"width";i:624;s:6:"height";i:624;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:19:"T_1_front-90x90.jpg";s:5:"width";i:90;s:6:"height";i:90;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:4:{s:4:"file";s:21:"T_1_front-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:11:"shop_single";a:4:{s:4:"file";s:21:"T_1_front-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:10:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";}}]]></wp:meta_value>
        </wp:postmeta>
    </item>

因此,我需要从库存报告中提取必要的东西 - 并将其放在该XML文件中的正确位置。解决这个问题的最佳方法是什么?感谢。

更新

我可以在获取报告的脚本中使用此php函数控制库存报告的格式:

/**
  * Get Report Action Sample
  * The GetReport operation returns the contents of a report. Reports can potentially be
  * very large (>100MB) which is why we only return one report at a time, and in a
  * streaming fashion.
  *   
  * @param MarketplaceWebService_Interface $service instance of MarketplaceWebService_Interface
  * @param mixed $request MarketplaceWebService_Model_GetReport or array of parameters
  */
  function invokeGetReport(MarketplaceWebService_Interface $service, $request) 
  {
      try {
              $response = $service->getReport($request);

                echo ("Service Response\n");
                echo ("=============================================================================\n");

                echo("        GetReportResponse\n");
                if ($response->isSetGetReportResult()) {
                  $getReportResult = $response->getGetReportResult(); 
                  echo ("            GetReport");

                  if ($getReportResult->isSetContentMd5()) {
                    echo ("                ContentMd5");
                    echo ("                " . $getReportResult->getContentMd5() . "\n");
                  }
                }
                if ($response->isSetResponseMetadata()) { 
                    echo("            ResponseMetadata\n");
                    $responseMetadata = $response->getResponseMetadata();
                    if ($responseMetadata->isSetRequestId()) 
                    {
                        echo("                RequestId\n");
                        echo("                    " . $responseMetadata->getRequestId() . "\n");
                    }
                }

                echo ("        Report Contents\n");
                echo (stream_get_contents($request->getReport()) . "\n");

                echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
     } catch (MarketplaceWebService_Exception $ex) {
         echo("Caught Exception: " . $ex->getMessage() . "\n");
         echo("Response Status Code: " . $ex->getStatusCode() . "\n");
         echo("Error Code: " . $ex->getErrorCode() . "\n");
         echo("Error Type: " . $ex->getErrorType() . "\n");
         echo("Request ID: " . $ex->getRequestId() . "\n");
         echo("XML: " . $ex->getXML() . "\n");
         echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
     }
 }

有没有人对什么分隔符有什么建议,以及在哪里 - 以便我可以有效可靠地解析数据?

重要的一句似乎是:

echo (stream_get_contents($request->getReport()) . "\n");

我需要一种方法来编辑流的内容,以便我可以添加分隔符,我认为......

更新

我阅读了stream_get_contents的手册,你可以使用一个参数告诉它要获取多少字节 - 以及一个偏移参数 - 我不能使用stream_get_contents多个时间 - 每次指定我想要的块,所以我可以跟踪那些数据是什么?

1 个答案:

答案 0 :(得分:0)

我意识到.txt文件是以制表符分隔的,这使得事情从超级难以变得非常容易 - 看起来它基本上是.csv但是由于某种原因,亚马逊将其输出为制表符分隔符.txt个文件。我找到了this堆栈问题,它提供了处理您可以生成和下载的亚马逊库存报告的完美方式(如果其他人有这个特定问题)。

我发现如果我为while循环的代码添加了一个计数器 - 我可以跟踪它正在查看的行 - 然后我这样做了:

if ($counter === 0 ) {
    echo implode(" ", $data);
}

$data表示数组形式的已解析行。事实证明,第0行只是每列的所有描述 - 如下所示:

  

item-name item-description listing-id卖家sku价格数量open-date image-url item-is-marketplace product-id-type zshop-shipping-fee item-note item-condition zshop-category1 zshop-browse -path zshop-storefront-feature asin1 asin2 asin3 will-ship-international加速运输zshop-boldface product-id bid-for-featured-placement add-delete pending-quantity fulfillment-channel

当我输出第1行时,令我高兴的是,这是他库存中第一项的所有信息:

  

上帝的眼睛(西格玛力量)[大众市场平装]作者:罗林斯,詹姆斯XXXXXX XXXXXX X.XX 0 2014-06-24 13:36:21 PDT y 1状态良好,盖子上有轻微折痕但非常干净。 1 0061785679 1 N 0061785679 0默认

然后我发现第2行代表第二项,依此类推......所以它们都非常整齐地打包 - 至少如果你使用的是str_getcsv PHP函数。

因为所有内容都是以制表符分隔的 - 从报表中获取所需的任何信息变得微不足道,因为即使列是空的 - 仍然会有一个标签表示它是空的...很好而整洁。