simplexml rss unexpected':'

时间:2012-06-06 11:23:55

标签: php rss simplexml

我有这个php(下面的问题)

<?php
$url = "http://woytest.blogspot.com/feeds/posts/default?alt=rss";
$rss = simplexml_load_file($url);
if($rss){
echo '<h1>'.$rss->channel->title.'</h1>';
$items = $rss->channel->item;
foreach($items as $item){
$title = $item->title;
$link = $item->link;
$img = $item->media:thumbnail->attributes()->url; // error on this line
$description = $item->description;
echo 'Post Title: '.$title.'<br/>';
echo 'Post Link: '.$link.'<br/>';
echo 'Post thumbnail: '.$img.'<br/>';
echo '<br/><br/><br/>';
}
}
?>

问题是media:thumbnail冒号在rss Feed上显示为<media:thumbnail url='...'>我怎样才能克服我尝试{'media:thumbnail'}替换media:thumbnail的问题PHP,但没有运气...我是新来的PHP所以,请你解释我的方式错误。

2 个答案:

答案 0 :(得分:2)

查看http://alisothegeek.com/2011/07/picking-apart-xml-feeds-and-namespaces-with-php-and-simplexml/

希望这对你有帮助;)

示例:

$entries = simplexml_load_file('http://woytest.blogspot.com/feeds/posts/default?alt=rss');
$namespaces = $entries->getNamespaces(true);

foreach ($entries->channel->item as $feeditem) 
{
    $thumbnail = $feeditem->children($namespaces['media'])->thumbnail;
    $attr = $thumbnail->attributes();

    echo '<pre>';
    echo "URL = {$attr['url']}, width = {$attr['width']}, height = {$attr['height']}\n";
    echo print_r($attr, true);
    echo '</pre>';
} 

答案 1 :(得分:0)

调用 print_r()来了解对象结构:

$url = "http://woytest.blogspot.com/feeds/posts/default?alt=rss";
$rss = simplexml_load_file($url);
echo "<pre>";
print_r($rss);
echo "</pre>";