【问题标题】:getting rss feeds in my website [duplicate]在我的网站上获取 RSS 提要 [重复]
【发布时间】:2013-12-08 04:56:43
【问题描述】:

下面的代码从 yahoo 获取 rss 提要,它可以工作,但我有一个小问题,当我擦代码时,它会返回提要,但它也会给我一个错误,例如 -

未定义的偏移量:第 8 行 /Applications/MAMP/htdocs/feedbackFtms/rss.php 中的 1

line 8 is - $foundImg = $findImg[1];

<div class="feed">
    <link rel="stylesheet" href="css/profilepage.css">

    <?php
        function getImageFromContext($context){

        $findImg = explode('<img src="',$context);
        $foundImg = $findImg[1];
        $findExt = explode('.jpg',$foundImg);
        $getIMG = $findExt[0].'.jpg';
        return $getIMG;
        }

       function getFeed($url){
        $x = simplexml_load_file($url);
        echo "<ul>";
        foreach($x->channel->item as $entry) {
            echo "<li><img src='".getImageFromContext($entry->description)."' /><a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
            }
            echo "</ul>";
        }
        getFeed("http://news.yahoo.com/rss/entertainment");
    ?>
    </div>

【问题讨论】:

  • 问题出在第 7 行,explode 没有找到任何可爆炸的地方。
  • 嘿迈克,请向我解释更多...不是真正的 php n 东西专家

标签: php rss gmail


【解决方案1】:

有些文章没有图片。

您可以按原样更改一些代码,我想它会起作用,但是为了更方便地使用正则表达式。

<?php
function getImageFromContext($context){

    $findImg = explode('<img src="',$context);
    if(isset($findImg[1])){
        $foundImg = $findImg[1];
        $findExt = explode('.jpg',$foundImg);
        $getIMG = '<img src="' . $findExt[0].'.jpg' . '" />';

        return $getIMG;
    }

    return '';
    }

function getFeed($url){
    $x = simplexml_load_file($url);

    echo "<ul>";
    foreach($x->channel->item as $entry) {
        echo "<li>".getImageFromContext($entry->description)."<a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
        }
        echo "</ul>";
    }
    getFeed("http://news.yahoo.com/rss/entertainment");

?>

</div> 

【讨论】:

  • 谢谢你,Artak..它现在可以工作了..我很感激。
【解决方案2】:

试试这个,

 foreach($x->channel->item as $entry) {
        echo "<li><img src='".getImageFromContext('<img src="'.$entry->description)."' /><a href='{$entry->link}' title='{$entry->title}'>{$entry->title}</a></li><br>";
        }
        echo "</ul>";
    }

我不确定您是否需要 getImageFromContext 函数中返回之前的最后两行。

【讨论】:

  • 嗨 scrooj.. 它确实消除了错误,但现在......它不显示缩略图,它显示默认的错误图像图标。
  • 嘿惊奇,刚刚做了一个编辑..现在应该没问题了。
  • 嘿 Scrooj ..也感谢您的帮助..您太棒了。它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2011-08-16
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多