下面的代码从雅虎获取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>
答案 0 :(得分:0)
试试这个,
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函数返回之前是否需要最后两行。
答案 1 :(得分:0)
有些文章没有图片。
你可以按原样改变你的代码,我想它会起作用但是这样使用正则表达式会更方便。
<?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>