php reader google 新

php reader google new

我在我的网站上为 Google 新闻安装了 php reader 以阅读前五篇文章,但是 (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 google 新闻 reader 的代码:

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