What's the best way to load an RSS feed into a PHP feed?
I also want to handle the media:content problems to display a image.
At this moment I have the following code but I don't know if this is the best.
<?php
$rss = new DOMDocument();
$rss->load('http://www.hln.be/rss.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'media' => $node->getElementsByTagName('media:content url')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 20;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
$image = $feed[$x]['media'];
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
echo '<img src="' . $image . '"/>';
}
?>
Check this:
<?php
$feed = simplexml_load_file('http://www.hln.be/rss.xml');
foreach ($feed->channel->item as $item) {
$title = (string) $item->title;
$description = (string) $item->description;
print '<div>';
printf(
'<h2>%s</h2><p>%s</p>',
$title,
$description
);
if ($media = $item->children('media', TRUE)) {
if ($media->content->thumbnail) {
$attributes = $media->content->thumbnail->attributes();
$imgsrc = (string)$attributes['url'];
printf('<div><img src="%s" alt="" /></div>', $imgsrc);
}
}
echo '</div>';
}
?>
You can try this:
<?php
$feed = simplexml_load_file('http://www.hln.be/rss.xml');
print_r($feed);
?>
This gives you object/array structure which you can use directly instead of communicating with additional layer(i.e. DOM) on top of the data.
Related
I want to ask, I see script this thread.
Script is still working fine but I want to include many feed and bring up on my website, how can I solve it?
example:
web1.domain.com/feed
web2.domain.com/feed
web3.domain.com/feed
...
I want to show feed subdomain to my top level domain.
$rss = new DOMDocument();
$rss->load('http://web.domain.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
$html = new DOMDocument();
$html->loadHTML($htmlStr);
//get the first image tag from the description HTML
$imgTag = $html->getElementsByTagName('img');
$img = ($imgTag->length==0)?'noimg.png':$imgTag->item(0)->getAttribute('src');
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'category' => $node->getElementsByTagName('category')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'image' => $img,
);
array_push($feed, $item);
}
$limit = 2;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$img = $feed[$x]['image'];
$category = str_replace(' & ', ' & ', $feed[$x]['category']);
$date = date('j F Y G:i', strtotime($feed[$x]['date']));
echo '<li>';
echo '<a href="'.$link.'" title="'.$title.'">';
echo '<div class="blog-widget-img left relative">';
echo '<img src="'.$img.'" height="100" width="165">';
echo '</div>';
echo '<div class="blog-widget-text left relative">';
echo '<span class="side-list-cat">'.$category.'</span>';
echo '<h2>'.$title.'</h2>';
echo '<span><time>'.$date.'</time></span>';
echo '</div>';
echo '</a>';
echo '</li>';
}
UPDATE:
I have found the solution check and it works
$my_feeds = array('http://web1.domain.com/feed',
'http://web2.domain.com/feed',
'http://web3.domain.com/feed');
foreach ($my_feeds as $my_feed)
{
$rss = new DOMDocument();
$rss->load ($my_feed); //load more feed with variable
$feed = array();
//star feed here
}
I have the following script
public function NewsRss() {
$rss = new DOMDocument();
libxml_use_internal_errors(true);
$rss->load('http://www.autoexpress.co.uk/feeds/all');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
$html = new DOMDocument();
$html->loadHTML($htmlStr);
$desc = $html->getElementsByTagName('p')->item(0)->nodeValue;
//var_dump($desc);
$imgTag = $html->getElementsByTagName('img');
$img = ($imgTag->length==0)?'noimg.png':$imgTag->item(0)->getAttribute('src');
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $desc,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'image' => $img,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="news-row-index">';
echo '<div class="details-index"><p><h5>'.$title.'</h5>';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<div class="img"><img src="'.$feed[$x]['image'].'" height="79" width="89"></div>';
echo '<p>'.$description.'</p></div>';
echo '</div>';
}
echo '<a style="margin-left:10px;" class="view-all-but" target="_blank" href="http://www.autoexpress.co.uk/feeds/all">View all</a>';
}
The problem is that us showing me a notice like this
Notice: Trying to get property of non-object in on line 10 which is
$desc = $html->getElementsByTagName('p')->item(0)->nodeValue; but if I hide the errors everything works fine, just that i am trying to clean up my script of errors, and on this case don't know exactly where to start. I know that -> should not be used many times.
Please help me on this, it will be much appreciated. I am kind of a noob in OOP and rss.
It looks like there are no p tags found, so the call to getElementsByTagName returns an empty list, and then you try to call item(0) on an empty list, which is why you get the error.
You could split it over 2 lines to check for no elements:
$p = $html->getElementsByTagName("p");
if($p->length > 0) {
$desc = $p->item(0)->nodeValue;
} else {
$desc = "";
}
I am trying to show the images in the following rss feed and would appreciate help from somebody please. I have tried getAttribute but am not sure how to format it or where in the code to put it, so I have taken it out to avoid confusion. The following code is working, but I need to add the code to display the images.
<?php
$rss = new DOMDocument();
$rss->load('http://xml.thinkspain.com/think-spain-feeds/spanish-news.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can get attribute by using getAttribute :
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')
I tried to create a PHP script in order to extract all the data but this is not working. I tried to get the im:price and other attributes but I don't know how to get them.
Can anyone help me correct my script?
<?php
$rss = new DOMDocument();
$rss->load('https://itunes.apple.com/fr/rss/topfreeapplications/limit=25/xml');
$feed = array();
foreach ($rss->getElementsByTagName('entry') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'price' => $node->getElementsByTagName('im')->item(0)->getAttribute('price')
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p> Price is '.$price.'</p>';
}
?>
In order to parse all of the im nodes you have to use namespaces like so.
$namespaces = $entry->getDocNamespaces(true);
$items = $entry->children($namespaces['im']);
Here is a sample to get you started:
<?php
$rss = "https://itunes.apple.com/fr/rss/topfreeapplications/limit=25/xml";
$xml = simplexml_load_file($rss);
foreach ($xml->entry as $entry){
$namespaces = $entry->getDocNamespaces(true);
$items = $entry->children($namespaces['im']);
$name = $items->name;
$summary = $entry->summary;
$price = $items->price;
$image = $items->image[1];
$rights = $entry->rights;
For all of the im nodes use $items->foo;. For all of the nodes without the im namespace use $entry->bar;
I am currently displaying an RSS feed and I am grabbing the title, link and date for each article. I would also like to grab the media:thumbnail for each article. Here is the PHP code I am using to retrieve the RSS feed.
<?php
$rss = new DOMDocument();
$rss->load('http://www.avfc.co.uk/rss/ptv/page/CustomArticleIndex/0,,10265~2282152,00.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 2;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div id="feed"><a target="_blank" href="'.$link.'" title="'.$title.' ">'.$title.'</a><br />';
echo '<a class="date">Posted on '.$date.'</a></div>';
}
?>
The RSS feed I am displaying is: http://www.avfc.co.uk/rss/ptv/page/CustomArticleIndex/0,,10265~2282152,00.xml
I'm not that good with PHP so does anybody know how I can go about doing this? Thanks.
Try that:
$thumb = $node->getElementsByTagName('media:thumbnail')->item(0)->attributes->getNamedItem('url')->nodeValue;