Get Tumbnail with google news rss feed using php - php

I can not get the tumbnail images using the following code. What i need to do to get a tumbnail images from google rss feed. Anyone can help me please ?
$news = simplexml_load_file('https://news.google.com/news?pz=1&cf=all&ned=en&hl=en&topic=n&output=rss');
$feeds = array();
$i = 0;
foreach ($news->channel->item as $item)
{
preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$item->description, $imageurl);
//preg_match('#src="([^"]+)"#', $item->description, $imageurl);
$parts = explode('<font size="-1">', $item->description);
$feeds[$i]['title'] = (string) $item->title;
$feeds[$i]['link'] = (string) $item->link;
$feeds[$i]['image'] = $imageurl[1];
$feeds[$i]['site_title'] = strip_tags($parts[1]);
$feeds[$i]['story'] = strip_tags($parts[2]);
$i++;
}
echo '<pre>';
print_r($feeds);
echo '</pre>';

Related

Count item tags from merged rss feed

I'm trying to count the title tag from a list of RSS feeds.
$urls = "http://www.engadget.com/rss.xml";
$xml = simplexml_load_file($urls);
$tags = array();
foreach($xml->channel->item as $item) {
$children = $item->title;
foreach ($children as $node) {
$tags[] = $node->getName();
}
}
$count= array_count_values($tags);
echo '<pre>';
print_r($count);
doing it this way above... it works but what if I had multiple URLs. how can I go about it?
Place the urls in an array and then just foreach around the urls array doing basically what you were already doing
$urls = ["http://www.engadget.com/rss.xml","http://www.engadget.com/rss.xml"];
$tags = array();
foreach ( $urls as $url ) {
$xml = simplexml_load_file($url);
foreach($xml->channel->item as $item) {
$children = $item->title;
foreach ($children as $node) {
$tags[] = $node->getName();
}
}
}
$count= array_count_values($tags);
echo '<pre>';
print_r($count);

php reader google new

I installed a php reader for Google News on my website to read the first five articles but (1) the images don't display, and (2) I would like only the first source article of the master title.
$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 list only one news and i want 5 news ;)
i found the solution and give my code for a php google news reader on specific query :
<?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
?>

Show RSS feed items based on other PHP script

I want to create an RSS feed in PHP based on other RSS feeds. That works.
But, Now I want to change how many items are shown in the new created feed. This based on calculation of the items of another feed.
I use this script for the calculation, which works fine:
<?php
$url = 'http://www.nu.nl/rss/Algemeen';
$xml = simplexml_load_file($url);
$tags = array();
foreach($xml->channel->item as $item) {
$children = $item->children(); // get all children of each item tag
foreach ($children as $node) {
$tags[] = $node->getName(); // get the node name of each children
}
}
$test = count($tags);
$count = array_count_values($tags); // count the values
?>
<?php
$mystring = count($tags);
$findme = '3';
$pos = strpos($mystyring, $findme);
?>
<?php
$artikel = ($mystring / 5);
echo $artikel;
?>
I use this script, magpierss, fow creating a new feed:
<?php
/**
* Setup
*
*/
$DOMAIN_NAME = 'http://sitename.nl/';
$FEED_URL = $DOMAIN_NAME . 'rss/full.php';
$SITE_TITLE = 'test';
$SITE_DESRIPTION = '-';
$SITE_AUTHOR = 'test2';
$RSS_CACHE = "/tmp/rsscache";
$RSS_CACHE_EXP = 3600;
$FEED_LIST = array(
'http://www.nu.nl/rss/Economie',
'http://www.nu.nl/rss/Internet'
);
/**
* Do not modify below this point
*
*/
define('MAGPIE_CACHE_DIR', $RSS_CACHE);
define('MAGPIE_CACHE_AGE', $RSS_CACHE_EXP);
define('MAGPIE_OUTPUT_ENCODING', 'utf-8');
// include required files
require_once ('magpierss-0.72/rss_fetch.inc');
include ('feedcreator.class.php');
/* Set RSS properties */
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = $SITE_TITLE;
$rss->description = $SITE_DESRIPTION;
$rss->link = $DOMAIN_NAME;
$rss->syndicationURL = $FEED_URL;
$rss->encoding = 'utf8';
/* Set Image properties
$image = new FeedImage();
$image->title = $SITE_TITLE . " Logo";
$image->url = $SITE_LOG_URL;
$image->link = $DOMAIN_NAME;
$image->description = "Feed provided by " . $SITE_TITLE . ". Click to visit.";
$rss->image = $image;
*/
function showSummary($url, $num = 10, $showfullfeed = false) {
global $rss, $DOMAIN_NAME, $SITE_AUTHOR, $SITE_TITLE;
$num_items = $num;
# $rss1 = fetch_rss($url);
if ($rss1) {
$items = array_slice($rss1->items, 0, $num_items);
foreach ($items as $item) {
$href = $item['link'];
$title = $item['title'];
if (!$showfullfeed) {
$desc = $item['description'];
} else {
$desc = $item['content']['encoded'];
}
// $desc .= '
//Copyright © '.$SITE_TITLE.'. All Rights Reserved.
//';
$pdate = $item['pubdate'];
$rss_item = new FeedItem();
$rss_item->title = $item['title'];
$rss_item->link = $item['link'];
$rss_item->description = $item['content']['encoded'];
$rss_item->date = $item['pubdate'];
$rss_item->source = $DOMAIN_NAME;
$rss_item->author = $SITE_AUTHOR;
$rss->addItem($rss_item);
}
} else {
echo "Error: Cannot fetch feed url - " . $url;
}
}
// Fetch all feeds
foreach($FEED_LIST as $v) showSummary($v);
// Sort items by date
function __usort($ad, $bd) {return strtotime($bd->date) - strtotime($ad->date);}
usort($rss->items, '__usort');
// Display items
$rss->saveFeed("RSS1.0", $RSS_CACHE . "/feed.xml");
How could I let the variable $artikel decide how many items are showed in the feed?

Retrieve RSS Feed Data with for loop

Forgive me because my knowledge of PHP is limited but I have this code which retrieves all the items from an RSS Feed but I now need it to be using a for loop instead of a foreach loop so that I can limit the amount of times it runs and what item number it starts from. How would I go about doing this? Thank you for your help in advance.
$urls = array("WordlideVideo" => "http://feeds.reuters.com/reuters/USVideoWorldNews");
$rss = fetch_rss($urls[$_GET['url']]);
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$video = $item['video'];
$titleLength = strlen($title);
if ($titleLength > 180) {
$title = substr($title, 0, 177);
$title = $title . "...";
} else {
$title = $title;
}
}
Assuming $rss->items is an array, you should be able to do something like the following:
$items = $rss->items;
$limit = count($items);
// put any logic here to reduce $limit if it's greater than your threshold
for($i=0; $i<$limit; $i++) {
$item = $items[$i];
// code as before inside foreach loop
}

How to get Picasa images from Picasa using RSS Feed

I use this code for get all images from my account picasa but without result :
$content = file_get_contents("https://picasaweb.google.com/data/feed/base/user/107745136468823194427?alt=rss&kind=photo&hl=fr&imgmax=1600");
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry => $value){
$title = $value->title;
$image = $value->enclosure->attributes()->url;
$urlimg = $image[0];
echo '<img src="'.$urlimg.'>"';
}
What the problem ? Thanks
Can you try with this code please?
<?php
$content = file_get_contents("https://picasaweb.google.com/data/feed/base/user/107745136468823194427?alt=rss&kind=photo&hl=fr&imgmax=1600");
$x = simplexml_load_string($content);
foreach($x->channel->item as $entry => $value){
$title = $value->title;
$image = $value->enclosure->attributes()->url;
$urlimg = $image[0];
echo '<img src="'.$urlimg.'>"';
}

Categories