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
}
Related
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>';
I'm trying to scrape a webpage with PHP Simple Html Dom,
but there seems to be a problem with the loop process which only loop once and then stops, which should do several loops and stop.
Here is the URL I want to scrape: http://anymart.c1.biz/index.html.
Please show me where is the problem and show me the correct code.
Here's my code :
require('simple_html_dom.php');
$url=('http://anymart.c1.biz/index.html');
$html = file_get_html($url);
$articles = [];
$i = 0;
foreach ($html->find('div.gallery-a table[width=745]') as $data)
{
if ($i > 5)
{
break;
}
$title = $data->find('tr.*[id] > td > a.gal_title', $i);
$item['title'] = $title->plaintext;
$url = $data->find('tr.*[id] > td > a.gal_title', $i);
$item['url'] = $url->href;
$image = $data->find('img.gal_thumb', $i);
$item['image'] = $image->src;
$articles[] = $item;
$i++;
}
$result = json_encode($articles, JSON_PRETTY_PRINT);
header('Access-Control-Allow-Origin: *');
header('Content-type: Application/JSON');
echo $result;
Thank you.
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?
So far my script is working fine, basically it gets all htm files, out puts results, however im using DOM to get the HTML title tag from each file, that's where im not get to get it in the random array.. (image basenames and htm basename files are the same (firstresult.htm has picture firstresult.jpg)
I hope the code I provide and answer will be useful
<?php
// loop through the images
$count = 0;
$filenamenoext = array();
foreach (glob("/mydirectory/*.htm") as $filename) {
$filenamenoext[$count] = basename($filename, ".htm");
$count++;
}
for ($i = 0; $i < 10; $i++) {
$random = mt_rand(1, $count - 1);
$cachefile = "$filename";
$contents = file($cachefile);
$string = implode($contents);
$doc = new DOMDocument();
#$doc->loadHTML($string);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
echo '<img class="image" src="'.$filenamenoext[$random].'.jpg" " />"'.$title.'"<BR><BR>';
}
?>
It looks like the $filename variable you use on the line $cachefile = "$filename"; hasn't been set. It's only defined in the foreach loop's scope.
You should change it to
$cachefile = $filenamenoext[$random] . '.htm';
Also, it's a better practice to use array_push() and count() functions, instead of using a counter and manually filling the array. At least the code is better looking and more readable.
<?php
// loop through the images
$count = 0;
$filenamenoext = array();
foreach (glob("/mydirectory/*.htm") as $filename) {
array_push($filenamenoext, basename($filename, ".htm"));
}
for ($i = 0; $i < 10; $i++) {
$random = mt_rand(1, count($filenamenoext) - 1);
$cachefile = $filenamenoext[$random] . '.htm';
$contents = file($cachefile);
$string = implode($contents);
$doc = new DOMDocument();
#$doc->loadHTML($string);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
echo '<img class="image" src="' . $filenamenoext[$random] . '.jpg" " />"' . $title . '"<BR><BR>';
}
?>
how to add an offset and limit to my PHP rss parser that returns the result as an object ?, here is what i have at the moment. it doesn't have any offset nor limit, how to approach this ?
class Rss
{
/*
*#access public
*#params url,int ''=default,int ''=default
*#usage input url,offset and limit,
*#returns content based onf the offset/limit input
*/
public function getFeed($url,$offset='',$limit=''){
$object = array();
$rss = simplexml_load_file($url);
foreach($rss->channel->item as $item){
$object[] = $item->title;
$object[] = $item->description;
$object[] = $item->link;
}
return $object;
}
}
Simpliest way
$limit = 10; $offset = 5;
$i=0; $taken=0;
foreach($rss->channel->item as $item){
if ($i>=$offset && $taken<$limit){
++$taken;
$object[] = $item->title;
$object[] = $item->description;
$object[] = $item->link;
}
//little optimization here
if ($taken == $limit)
break;
++$i;
}
Of course you can store $limit and $offset as object properties, or get them elsewhere.
how about a single counter thing? set offset/limit as needed
public function getFeed($url,$offset='',$limit=''){
$object = array();
$rss = simplexml_load_file($url);
$offset = 3; $limit = 8; $counter = 0;
foreach($rss->channel->item as $item){
$counter++;
if ($counter > $offset && $counter < $limit) {
$object[] = $item->title;
$object[] = $item->description;
$object[] = $item->link;
}
}
return $object;
}
You can use SimpleXMLElement::xpath. This way you don't have to traverse all items just for counting things.
public function getFeed($url, $offset = 1, $limit = -1){
$object = array();
$rss = simplexml_load_file($url);
$limitCriteria = '';
if ($limit > 0) {
$limitCriteria = 'and position() <= ' . ((int)$offset + (int)$limit + 1);
}
foreach($rss->xpath(sprintf('//item[position() >= %s %s]', (int)$offset, $limitCriteria)) as $item){
$object[] = $item->title;
$object[] = $item->description;
$object[] = $item->link;
}
return $object;
}