Sorting XML output - php

Here is the PHP im using to read XML:
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->image as $image) {
$id = $image->id;
$link = $image->href;
$src = $image->source;
$title = $image->title;
$alt = $image->alt;
echo "<a href='$link'><img src='$src' alt='$alt' title='$title' /></a>";
}
The XML contains an ID: $id = $image->id;
I need to sort the output by that ID. How can I do this within the foreach?

if id is unique i will do something like
<?php
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
foreach ($xml->image as $image) {
$id = $image->id;
$images[$id]['id'] = $id;
$images[$id]['link'] = $image->href;
$images[$id]['src'] = $image->source;
$images[$id]['title'] = $image->title;
$images[$id]['alt'] = $image->alt;
}
ksort ($images);
foreach ($images as $image){
echo "<a href='".$image['link']."'><img src='".$image['src']."' alt='".$image['alt']."' title='".$image['title']."' /></a>";
}
?>
And if id is not unique then try this
<?php
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
$ids = array();
foreach ($xml->image as $image) {
$images[]['id'] = $image -> id;
$images[]['link'] = $image->href;
$images[]['src'] = $image->source;
$images[]['title'] = $image->title;
$images[]['alt'] = $image->alt;
$ids[] = $image -> id;
}
array_multisort($ids, SORT_ASC, $images);
foreach ($images as $image){
echo "<a href='".$image['link']."'><img src='".$image['src']."' alt='".$image['alt']."' title='".$image['title']."' /></a>";
}
?>
These are some quick ones. But these will do the job.

Related

Get Tumbnail with google news rss feed using 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>';

Extract href from html page using php

I trying to extract the news headlines and the link (href) of each headline using the code bellow, but the link extraction is not working. It's only getting the headline. Please help me find out what's wrong with the code.
Link to page from which I want to get the headline and link from:
http://web.tmxmoney.com/news.php?qm_symbol=BCM
<?php
$data = file_get_contents('http://web.tmxmoney.com/news.php?qm_symbol=BCM');
$dom = new domDocument;
#$dom->loadHTML($data);
$dom->preserveWhiteSpace = true;
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//div');
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('span');
$newstitle = $cols->item(0)->nodeValue;
$link = $cols->item(0)->nodeType === HTML_ELEMENT_NODE ? $cols->item(0)->getElementsByTagName('a')->item(0)->getAttribute('href') : '';
echo $newstitle . '<br>';
echo $link . '<br><br>';
}
?>
Thanks in advance for your help!
Try to do this:
<?php
$data= file_get_contents('http://web.tmxmoney.com/news.php?qm_symbol=BCM');
$dom = new DOMDocument();
#$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
$hrefs= $xpath->query('/html/body//a');
for($i = 0; $i < $hrefs->length; $i++){
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$url = filter_var($url, FILTER_SANITIZE_URL);
if(!filter_var($url, FILTER_VALIDATE_URL) === false){
echo ''.$url.'<br />';
}
}
?>
I have found the solution. Here it goes:
<?php
$data = file_get_contents('http://web.tmxmoney.com/news.php?qm_symbol=BCM');
$dom = new domDocument;
#$dom->loadHTML($data);
$dom->preserveWhiteSpace = true;
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//div');
foreach ($rows as $row) {
$cols1 = $row->getElementsByTagName('a');
$link = $cols1->item(0)->nodeType === XML_ELEMENT_NODE ? $cols1->item(0)->getAttribute('href') : '';
$cols2 = $row->getElementsByTagName('span');
$title = $cols2->item(0)->nodeValue;
$source = $cols2->item(1)->nodeValue;
echo $title . '<br>';
echo $source . '<br>';
echo $link . '<br><br>';
}
?>

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?

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.'>"';
}

how can echo item in php foreach only at First item

I have an array $result and I want to echo $image for the first item only. My code this is:
<?php
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
}
?>
i want echo $image in first item and don't echo it Second, or third, etc item
$item = reset($array);
// do stuff with $item here
^ gets the first item of the array without removing it.
Or just break before the closing } in your code.
<?php
$first = true;
foreach ($result as $item)
{
$image = ($first) ? $item->image : '';
$id = $item->id;
$cat = $item->catid;
$first = false;
}
?>
if you want display only first item, you can use array_shift
$item = array_shift($result);
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
You can count your iteration and print only at first run
$i=o;
foreach ($result as $item)
{
$image = $item->image;
$id = $item->id;
$cat = $item->catid;
if($i==0)
{
echo $image;
}
$i++;
}
$arr = array('foo', 'baz', 'baz');
reset($arr);
$first = current($arr);
echo $first->image; // outputs 'foo'
Try this:
<?php
foreach ($result as $item)
{
$image = $result[0]->image;
$id = $item->id;
$cat = $item->catid;
}
?>

Categories