我正在尝试做以下工作:使用核心(没有任何框架或插件)的& AMAZON
Attachments:
代码示例:
$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)标题如何提高您的领导能力和管理技能-业务经理的有效策略)
所需的帮助:从上述两个数组元素中提取出代码将为输出表提供以下列
发布于 2017-09-07 06:47:52
可以通过使用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
)。
https://stackoverflow.com/questions/46089290
复制相似问题