Google News Feed Parser PHP


SUBMITTED BY: Guest

DATE: July 21, 2015, 10:34 p.m.

FORMAT: PHP

SIZE: 4.0 kB

HITS: 508

  1. <?php
  2. error_reporting(0);
  3. date_default_timezone_set("America/Managua"); /* Zona horaria para la fecha de las noticias */
  4. $chars = 250; /* Número de caracteres en la descripción de cada noticia */
  5. $query = "Nicaragua"; /* Término de busqueda en que se relacionarán las noticias */
  6. $ned = "ni"; /* Localización en que se relacionarán las noticias */
  7. $hl = "ni"; /* Lenguaje en que se relacionarán las noticias */
  8. $gnd = "news.google.com.mx"; /* URL de Google Noticias con nombre de dominio según país */
  9. function fechaesp($texto) {
  10. $fechaesp = array(
  11. 'Monday' => 'Lunes',
  12. 'Tuesday' => 'Martes',
  13. 'Wednesday' => 'Miércoles',
  14. 'Thursday' => 'Jueves',
  15. 'Friday' => 'Viernes',
  16. 'Saturday' => 'Sábado',
  17. 'Sunday' => 'Domingo',
  18. 'January' => 'Enero',
  19. 'February' => 'Febrero',
  20. 'March' => 'Marzo',
  21. 'April' => 'Abril',
  22. 'May' => 'Mayo',
  23. 'June' => 'Junio',
  24. 'July' => 'Julio',
  25. 'August' => 'Agosto',
  26. 'September' => 'Septiembre',
  27. 'October' => 'Octubre',
  28. 'November' => 'Noviembre',
  29. 'December ' => 'Diciembre'
  30. );
  31. foreach($fechaesp as $a => $b) $texto = str_replace($a,$b,$texto);
  32. return $texto;
  33. }
  34. ?>
  35. <html>
  36. <head>
  37. <title>Noticias <?php echo $query ?></title>
  38. <style type="text/css">
  39. .newsarticle {
  40. font-size:14px;
  41. margin-bottom: 30px;
  42. width: 500px;
  43. }
  44. .newsarticle div {
  45. font-size:14px;
  46. }
  47. .newsarticle h5 {
  48. margin:0;
  49. font-weight: 400;
  50. color:#888888;
  51. }
  52. a.rmorelink, a.rmorelink:visited {
  53. font-size:12px;
  54. color: #555555;
  55. text-decoration:none;
  56. float:right;
  57. text-transform:uppercase;
  58. }
  59. a.rmorelink:hover {
  60. color: #000000;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <?php
  66. $news = simplexml_load_file('http://'.$gnd.'/news/feeds?pz=1&cf=all&ned='.$ned.'&hl='.$hl.'&q='.$query.'&output=rss'); /* URL del Feed RRS de Google Noticias */
  67. $feeds = array();
  68. $i = 0;
  69. foreach ($news->channel->item as $item)
  70. {
  71. if ($i < 10) {
  72. preg_match('@src="([^"]+)"@', $item->description, $match);
  73. $parts = explode('<font size="-1">', $item->description);
  74. $feeds[$i]['title'] = mb_convert_encoding((string) $item->title, "HTML-ENTITIES", "UTF-8");
  75. $feeds[$i]['link'] = (string) $item->link;
  76. $feeds[$i]['link'] = preg_replace('#http://news.google.com(.*?)url=(.*?)#s', '',$feeds[$i]['link']);
  77. $feeds[$i]['image'] = $match[1];
  78. $feeds[$i]['image'] = str_replace('//', 'http://', $feeds[$i]['image']);
  79. $feeds[$i]['story'] = mb_convert_encoding(strip_tags($parts[2]), "HTML-ENTITIES", "UTF-8");
  80. $stringCut = substr($feeds[$i]['story'], 0, $chars);
  81. $feeds[$i]['story'] = substr($stringCut, 0, strrpos($stringCut, ' ')).' ...<br /><a class="rmorelink" href="'.$feeds[$i]['link'].'">Leer Más »</a>';
  82. $feeds[$i]['date'] = (string) $item->pubDate;
  83. $feeds[$i]['date'] = date("l, d F o g:i A", strtotime($feeds[$i]['date']));
  84. $feeds[$i]['date'] = fechaesp($feeds[$i]['date']);
  85. echo '<div class="newsarticle"><a href="';
  86. print($feeds[$i]['link']);
  87. echo '" taget="_blank"><b>';
  88. print($feeds[$i]['title']);
  89. echo '</b></a><br /><h5>';
  90. print($feeds[$i]['date']);
  91. echo '</h5><div>';
  92. echo '<img src="'.$feeds[$i]['image'].'" />';
  93. print($feeds[$i]['story']);
  94. echo '</div></div>';
  95. $i++;
  96. }
  97. }
  98. ?>
  99. </body>
  100. </html>

comments powered by Disqus