我正在创建一个使用大量rss源代码的php脚本。它将rss提要放在数据库中。 缩略图/图像放在xml文件中的方式有多种。我有一个特定方式的问题。
当xml文件包含以下内容时:Source
...
<media:content type="image/jpeg" url="http://static3.hln.be/static/photo/2015/1/8/6/20150405151722/crop_7613016.jpg">
<media:thumbnail url="http://static3.hln.be/static/photo/2015/1/8/6/20150405151722/crop_7613016.jpg"/>
</media:content>
...
出于某种原因,看起来MagPie没有得到那个元素。如果我var_dump该项目,它看起来像这样:
...
["media"]=> array(1) { ["content"]=> string(1) " " } ["content"]=> string(1) " "
...
任何人都知道我如何提取缩略图元素? 提前谢谢。
更新 - (某些示例代码):
$rss = fetch_rss('http://www.hln.be/rss.xml');
foreach ($rss->items as $item )
{ //loop through rss feed
var_dump($item); //the var_dump without media elements
return $array = $arrayName = array(
'title' => $item['title'],
'url' => $item['link'],
...);
putDataInDatabase(); //put everything in the database
}
更全面地了解var_dump
array(10) { ["title"]=> string(62) "Isinbayeva ..."" ["link"]=> string(23) "http://s.hln.be/2276986" ["description"]=> string(141) "Yelena Isinbayeva ..." ["pubdate"]=> string(29) "Sun, 05 Apr 2015 13:06:00 GMT" ["author"]=> string(8) "redactie" ["guid"]=> string(23) "http://s.hln.be/2276986" ["media"]=> array(1) { ["content"]=> string(1) " " } ["content"]=> string(1) " " ["summary"]=> string(141) "Yelena Isinbayeva wil... " ["date_timestamp"]=> int(1428239160) }
答案 0 :(得分:2)
元素在该数组中,例如您可以访问内容缩略图的URL,如下所示:
$item['media']['content_thumbnail@url']
将它放在你的例子的视角中:
$rss = fetch_rss('http://www.hln.be/rss.xml');
foreach ($rss->items as $item) { //loop through rss feed
var_dump($item['media']['content_thumbnail@url']);
}
提供以下输出:
string(79) "http://static3.hln.be/static/photo/2015/2/10/14/20150405204108/crop_7613834.jpg"
string(78) "http://static0.hln.be/static/photo/2015/1/9/13/20150405201321/crop_7613833.jpg"
string(77) "http://static2.hln.be/static/photo/2015/7/0/8/20150405203748/crop_7613858.jpg"
string(77) "http://static2.hln.be/static/photo/2015/0/6/8/20150405200321/crop_7613813.jpg"
string(79) "http://static2.hln.be/static/photo/2015/17/6/10/20150405200509/crop_7613830.jpg"
string(77) "http://static1.hln.be/static/photo/2015/7/9/7/20150405195208/crop_7613782.jpg"
string(78) "http://static2.hln.be/static/photo/2015/0/15/7/20150405193052/crop_7613737.jpg"
...
这是媒体元素的整体结构:
Array
(
[content#] => 1
[content@] => type,url
[content@type] => image/jpeg
[content@url] => http://static3.hln.be/static/photo/2015/2/10/14/20150405204108/crop_7613834.jpg
[content] =>
[content_thumbnail#] => 1
[content_thumbnail@] => url
[content_thumbnail@url] => http://static3.hln.be/static/photo/2015/2/10/14/20150405204108/crop_7613834.jpg
)