0

我有这个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出现<media:thumbnail url='...'>在 rss 提要上的冒号如何克服我尝试{'media:thumbnail'}仅替换media:thumbnailphp 中的冒号但没有运气的问题...我是 php 新手,请您解释一下我的错误的方法。

4

2 回答 2

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>';
} 
于 2012-06-06T11:33:14.083 回答
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>";
于 2012-06-06T11:32:54.390 回答