首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP解析:响应

PHP解析:响应
EN

Stack Overflow用户
提问于 2017-09-07 06:27:52
回答 1查看 1.6K关注 0票数 0

我正在尝试做以下工作:使用核心(没有任何框架或插件)的& AMAZON

  1. 生成有符号的URL
  2. 使用签名的url查询amazon的某些关键字
  3. 以XML格式获取响应
  4. 以HTML的表格格式解析XML &显示 到目前为止,我已经能够完成步骤3,但无法完成解析XML的步骤4&显示数据。

Attachments:

代码示例:

代码语言:javascript
运行
复制
 $file = file_get_contents($request_url);
    $xml = simplexml_load_string($file);
    print_r($xml);

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

( amazon.in/Chanakyas-Secrets-Leadership-Sivanandhan-Radhakrishnan/dp/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 amazon.in/gp/registry/wishlist/add-item.html?asin.0=8184954018&SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 => SimpleXMLElement对象( ASIN => 8184954018 DetailPageURL => ItemLinks => SimpleXMLElement Object )( ItemLink => Array ( => SimpleXMLElement Object ( Description => Add To Wishlist URL => => ))1 => =>对象( Description =>告诉好友URL amazon.in/gp/pdp/taf/8184954018 )( ?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 )2 => SimpleXMLElement Object ( Description => All Customer Reviews => )3 => SimpleXMLElement Object ( Description => All Book => amazon.in/gp/offer-listing/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 )) ItemAttributes => SimpleXMLElement Object (作者=> D. Sivanandhan Radhakrishnan Pillai )(作者=> D.Sivanandhan Radhakrishnan Pillai) 1 amazon.in/Improve-Your-Leadership-Management-Skills-ebook/dp/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW SimpleXMLElement对象( ASIN => B00EA0Q3PW DetailPageURL => ItemLinks => SimpleXMLElement Object )( ItemLink => Array ( => SimpleXMLElement Object ( Description =>添加到Wishlist 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对象( Description Tell a Friend URL amazon.in/gp/pdp/taf/( B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW )2 => SimpleXMLElement Object ( Description => All Customer Reviews => )3 => SimpleXMLElement Object ( Description => All Customer => amazon.in/gp/offer-listing/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW )) ItemAttributes => SimpleXMLElement Object (作者=> Meir Liraz制造商=> Liraz Publishing SimpleXMLElement)标题如何提高您的领导能力和管理技能-业务经理的有效策略)

所需的帮助:从上述两个数组元素中提取出代码将为输出表提供以下列

  • 阿辛
  • 标题
  • 作者
  • 详细信息URL
  • 客户评论URL
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 06:47:52

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

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

代码语言:javascript
运行
复制
<?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 )。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46089290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档