douhuan1380 2012-06-06 11:23
浏览 24
已采纳

simplexml rss unexpected':'

I have this php (problem underneath)

<?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/>';
}
}
?>

the problem is the media:thumbnail with the colon which appears as <media:thumbnail url='...'> on the rss feed how can i overcome the problem i have tried {'media:thumbnail'} replacing just the media:thumbnail in the php but had no luck... I am new to php so could you please explain the error of my ways to.

  • 写回答

2条回答 默认 最新

  • dongmei5168 2012-06-06 11:33
    关注

    Take a look at http://alisothegeek.com/2011/07/picking-apart-xml-feeds-and-namespaces-with-php-and-simplexml/

    Hopefully this should help you ;)

    Example:

    $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']}
    ";
        echo print_r($attr, true);
        echo '</pre>';
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试