php阅读器谷歌新

时间:2018-03-10 13:22:18

标签: 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++;
}

1 个答案:

答案 0 :(得分:0)

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

我找到了解决方案,并根据特定查询提供了我的Android google新闻阅读器代码:

<?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
 	?>