PHP XML解析:Amazon Product API响应

时间:2017-09-07 06:27:53

标签: php xml

我正在尝试执行以下操作:使用核心PHP(没有任何框架或插件)& AMAZON API

  1. 生成签名网址
  2. 使用签名网址
  3. 查询亚马逊的某些关键字
  4. 以XML格式获取回复
  5. 解析XML&以HTML格式显示表格格式

    到目前为止,我已经能够完成第3步,但无法完成解析XML&的步骤4。显示数据。

  6. 附件:

    代码示例:

     $file = file_get_contents($request_url);
        $xml = simplexml_load_string($file);
        print_r($xml);
    

    输出:我正在粘贴我从print_r上面得到的10个数组元素中的前2个;

      

    ([0] => SimpleXMLElement对象([ASIN] => 8184954018 [DetailPageURL] => amazon.in/Chanakyas-Secrets-Leadership-Sivanandhan-Radhakrishnan/dp/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag= 30061200-21& linkCode = xm2& camp = 2025& creative = 165953& creativeASIN = 8184954018 [ItemLinks] => SimpleXMLElement对象([ItemLink] =>数组([0] => SimpleXMLElement对象([说明] => ;添加到心愿单[URL] => amazon.in/gp/registry/wishlist/add-item.html?asin.0=8184954018&SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025& creative = 165953& creativeASIN = 8184954018)[1] => SimpleXMLElement对象([说明] =>告诉朋友[URL] => amazon.in/gp/pdp/taf/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag= 30061200-21& linkCode = xm2& camp = 2025& creative = 165953& creativeASIN = 8184954018)[2] => SimpleXMLElement对象([说明] =>所有客户评论[URL] => am azon.in/review/product/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018)[3> => SimpleXMLElement对象([描述] =>所有商品[URL] => amazon.in/gp/offer-listing/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953& ; creativeASIN = 8184954018)))[ItemAttributes] => SimpleXMLElement对象([作者] => D. Sivanandhan Radhakrishnan Pillai [制造商] => Jaico出版社[ProductGroup] => Book [Title] => Chanakya的7个领导秘诀)

         

    [1] => SimpleXMLElement对象([ASIN] => B00EA0Q3PW [DetailPageURL] => amazon.in/Improve-Your-Leadership-Management-Skills-ebook/dp/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2& camp = 2025& creative = 165953& creativeASIN = B00EA0Q3PW [ItemLinks] => SimpleXMLElement对象([ItemLink] =>数组([0] => SimpleXMLElement对象([描述] =>添加到愿望清单[URL] = > amazon.in/gp/registry/wishlist/add-item.html?asin.0=B00EA0Q3PW&SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW) [1] => SimpleXMLElement对象([说明] =>告诉朋友[URL] => amazon.in/gp/pdp/taf/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2& camp = 2025& creative = 165953& creativeASIN = B00EA0Q3PW)[2] => SimpleXMLElement对象([描述] =>所有客户评论[URL] => www.amazon.in/review/pro duct / B00EA0Q3PW?SubscriptionId = AKIAJ5B7EPH4TJ6YEUUQ& tag = 30061200-21& linkCode = xm2& camp = 2025& creative = 165953& creativeASIN = B00EA0Q3PW)[3] => SimpleXMLElement对象([描述] =>所有商品[URL] => amazon.in/gp/offer-listing/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953& ; creativeASIN = B00EA0Q3PW)))[ItemAttributes] => SimpleXMLElement对象([作者] => Meir Liraz [制造商] => Liraz出版[ProductGroup] =>电子书[标题] =>如何提高您的领导能力和管理技能 - 业务经理的有效策略)

    我需要什么帮助:在上面的两个数组元素中,PHP代码将获得带有以下列的输出表

    • ASIN
    • 标题
    • 作者
    • 详情网址
    • 客户审核网址

1 个答案:

答案 0 :(得分:1)

使用DOMDocument而不是SimpleXML可以轻松处理所需内容。默认命名空间(ItemSearchResponse元素中的xmlns定义)使SimpleXML不那么简单。

但首先,以下代码应该有帮助...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$file = file_get_contents("t1.xml");
$xml = new DOMDocument();
$xml->loadXML($file);
$items = $xml->getElementsByTagName("Items")[0];
foreach ( $items->getElementsByTagName("Item") as $item )    {
    echo "ASIN:".$item->getElementsByTagName("ASIN")[0]->nodeValue.PHP_EOL;
    echo "Author:".$item->getElementsByTagName("Author")[0]->nodeValue.PHP_EOL;
}

所有这一切都是首先提取<items>元素,并使用循环来获取其中的每个<item>元素。在循环中,您可以看到它使用适当的名称提取单个元素数据。

每次调用getElementsByTagName都会返回一个元素数组,这就是为什么你必须说只使用第一个元素(假设这是正确的,如果没有,你可以foreach对它们进行处理。)