PHP阅读器谷歌新

  • 本文关键字:谷歌 PHP php rss
  • 更新时间 :
  • 英文 :


我在我的网站上安装了谷歌新闻的PHP阅读器来阅读前五篇文章,但(1(图像不显示,(2(我只想要主标题的第一篇源文章。

$news = simplexml_load_file('https://news.google.com/news?q=guadeloupe&output=rss&hl=fr&ned=fr&num=5');
$feeds = array();
$i = 0;
foreach ($news->channel->item as $item)
{
$parts = explode('<td', $item->description);
$titre = explode('<div class="lh">', $item->description);
$feeds[$i]['description'] = (string) $item->description;
$feeds[$i]['title'] = (string) $item->title;
$feeds[$i]['link'] = (string) $item->link;
if (isset($parts[1])) {
$feeds[$i]['site_title'] = strip_tags($parts[1]);
}
$RSS_title = (string) $item->title;
$RSS_link = (string) $item->link;
if (isset($parts[2])) {
$RSS_part2 = $parts[2];
$RSS_part2 = str_replace('valign="top" class="j">','',$RSS_part2);
$RSS_part2 = str_replace('<a href=','<a class="lienviolet15B" style="font-size:14px;" target="_blank" href=',$RSS_part2);
echo "$RSS_part2";
}
$i++;
}

RiggsFolly,num=1 只列出一条新闻,我想要 5 条新闻;)

我找到了解决方案,并为特定查询的PHP谷歌新闻阅读器提供了我的代码:

<?php
 // clean cut function
 function cleanCut($string,$length,$cutString = '...'){
	if(strlen($string) <= $length)	{
		return $string;	}
		$str = substr($string,0,$length-strlen($cutString)+1);
		return substr($str,0,strrpos($str,' ')).$cutString;
	}
 //// google news => list 5 news on special query, here "formule 1" 
 //// if query have space or special chars use urlencode($myquery)
 //// replace language value in rss url (here "fr" for french)
 $myquery = "formule 1";
 $myquery = urlencode($myquery);
 $news = simplexml_load_file('https://news.google.com/news?q='.$myquery.'&output=rss&hl=fr&ned=fr&num=5');
 $feeds = array();
 $i = 0;
 foreach ($news->channel->item as $item) {
 	preg_match('@src="([^"]+)"@', $item->description, $match);
 	$parts = explode('<font size="-1">', $item->description);
 	$feeds[$i]['title'] = (string) $item->title;
 	$feeds[$i]['link'] = (string) $item->link;
 	$feeds[$i]['image'] = $match[1];
 	$feeds[$i]['site_title'] = strip_tags($parts[1]);
 	$feeds[$i]['story'] = strip_tags($parts[2]);
 	$i++;
 	$RSS_title = (string) $item->title;
 	$RSS_title = substr($RSS_title, 0, strpos($RSS_title, "-")); // delete source site in title
 	$RSS_title = cleanCut($RSS_title, 60); 	// (option) clean cut title after 60 char
 	$RSS_link = (string) $item->link; 			// extract link
 	$RSS_story = strip_tags($parts[2]); 		// extract begin article
 	$RSS_story = cleanCut($RSS_story, 260); // (option) clean cut article after 260 char
 	$RSS_image = $match[1]; 								// extract image
 	$RSS_source = strip_tags($parts[1]); 		// extract source site
 	if ($RSS_image!="") { // only article with image
 	echo "<div class="row" style="margin-bottom:20px;">";
 	echo "<div class="col-xs-12">";
 	echo "<img src="".$RSS_image."" style="width:80px;float:left;margin:0px 10px">";
 	echo "<a class="lienviolet15B" style="font-size:14px;" target="_blank" href="".$RSS_link."">";
 	echo "".$RSS_title."";
 	echo "</a><br>";
 	echo "".$RSS_story."<br>";
 	echo "<div class="pull-right"><i>".$RSS_source."</i></div>";
 	echo "</div></div>";
	}
 	}
 	//echo '<pre>'; print_r($feeds); echo '</pre>'; // rss brut
 	?>

最新更新