Rss feed with PHP - php

Hi can Anyone tell me how to process this rss feed in php
http://www.ft.com/rss/companies/travel-leisure
when i am executing the below lines
$rss = new DOMDocument();
$rss->load('http://www.ft.com/rss/companies/travel-leisure');
it is giving an error
A PHP Error was encountered
Severity: Warning
Message: DOMDocument::load(): Opening and ending tag mismatch: link line 8 and head in http://www.ft.com/rss/companies/travel-leisure, line: 11
Thanks

If you request http://www.ft.com/rss/companies/travel-leisure without a User-Agent HTTP request header, you can get an error message back (under a 200 OK status).
This is a bug in FT's website.
As a work around, I suggest using cURL to fetch the data, and then feed a string into DOMDocument.

Why not use a dedicated library like SimplePie to process your RSS feed ?
$feed = new SimplePie('http://www.ft.com/rss/companies/travel-leisure');
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items() as $item) {
$permalink = $item->get_permalink();
$title = $item->get_title();
// Do what you want...
}
I have not tested this code, it's just to show you an example of use. Here the documentation and an example for more explanations.

Related

how to validate RSS feed URL in simplpie library

I want to validate RSS feed URL before its process for parsing. I am using willvincent/feeds library for this.
$feed = Feeds::make($rssurl);
$items = $feed->get_items();
I am parsing RSS feed like above.
So how can validate the RSS feed URL before parsing using willvincent/feeds library.
'willvincent/feeds' is just a service provider for Laravel.
You should refer to SimplePie documentation, and use the error() method to check if url throws errors. This method holds the error description.
$feed = Feeds::make('http://some-bogus-feed-url');
if($feed->error()){
//handle the feed error
}

PHP wont load XML file [duplicate]

This question already has answers here:
DOMDocument - Load xml rss - failed to open stream
(2 answers)
Closed 7 years ago.
I am writing a web app and I am trying access a RSS feed from the financial times. However when I run my code I get the following error. I was hoping someone could tell me where I've went wrong.
function getFtRSS(){
//Create a object to parse XML input from Finanial Time's UK companies RSS feed
$rssFeed = new DOMdocument();
//Gather the information and load it into the object
$rssFeed->load('http://www.ft.com/rss/companies/uk');
$articles = array();
//Lookp through all elements in the XML document
foreach($rssFeed->getElementsByTagName('item') as $newsArtcle){
$title = $newsArtcle->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;
echo '<li class="list-group-item news-item" onclick="window.location=\'$link\'">
<h3 class=\'top-element\'>$title</h3>
<h4>$desc</h4>
<h5>$date</h5>
</li>';
}
}
I get the following error from my program:
Warning: DOMDocument::load(http://www.ft.com/rss/companies/uk): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/ubuntu/workspace/MOT/includes/functions.php on line 46
Have you checked the request out of the PHP ? I noticed that they don't accept requests with bad UA check the screenshot below;
Update: Yes they do not accept bad UA, you should change your data fetching method.

How to implement the SimplePie force_feed function in WordPress

When I try to parse a specific RSS feed using the native RSS widget for WordPress I receive the following error:
RSS Error: A feed could not be found at
http://trustbox.trustpilot.com/r/harringtonbrooks.co.uk.xml. A feed
with an invalid mime type may fall victim to this error, or SimplePie
was unable to auto-discover it.. Use force_feed() if you are certain
this URL is a real feed.
I have validated the feed using the http://validator.w3.org/feed/ and it states that the feed is valid. I belive that the mime type is also correct for the feed - type="application/rss+xml"
After consulting the SimplePie documentation this code is suggested as the fix - however I am clueless as to where this code would actually be implemeted within the WordPress installation.
$feed = new SimplePie();
$feed->set_feed_url('MY FEED URL');
$feed->force_feed(true);
$feed->init();
$feed->handle_content_type();
echo $feed->get_title();
Alternatively are there any other suggestions for getting this RSS feed to actually be parsed by the reader?

How can i validate an XML feed before parsing it with simplexml_load_file in PHP?

I am doing this at the moment
<?php
$xml = simplexml_load_file('www.tompeters.com/atom.xml');
print_r($xml);
?>
but it returns this error
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "www.tompeters.com/atom.xml"
I think it is because the feed isn't valid XML?
So how can I validate it so I can parse the feed?
Thanks
Parsing the feed is validating it.
Anyway, the error message looks more like that you don't have access to that file. I don't know PHP, but I would have expected at least
simplexml_load_file('http://www.tompeters.com/atom.xml');

YouTube XML Feed Error

I've got a page on a website that's pulling my favorites feed from YouTube and embedding them into site.
The problem is it's working 80% of the time, but the other 20% of the time i'm getting errors on the page - nothing in the code is changing to cause this, so i'm wondering what might be causing this, or if there's a better way to do what i'm doing...
The Error I'm gettings is a 403 Forbidden when retrieving the XML feed... here's what it looks like (note: the line numbers won't match exactly, because i've simplified the code sample below.
The XML feed in question is here:
https://gdata.youtube.com/feeds/api/users/umarchives/favorites
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/users/umarchives/favorites) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/umarchives/favorites" in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: Invalid argument supplied for foreach() in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 47
Here's the code i'm using:
<?php
// set feed URL
$YouTubeUsername = "umarchives";
$feedURL = "https://gdata.youtube.com/feeds/api/users/".$YouTubeUsername."/favorites";
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->content->attributes();
$videoURL = $attrs['url'];
$videoURL = preg_replace('/\?.*/', '', $videoURL);
$videoURL = str_replace("/v/","/embed/",$videoURL);
$videoTitle = $media->group->title;
echo "<iframe class='youtube-player' width='300' height='225' src='$videoURL'></iframe>\n";
echo "<br>\n";
}
?>
You should be validating the result of $sxml = simplexml_load_file($feedURL); per the Google error validation docs. Then you can print out the actual message that comes along with the 403 code, or possibly decide to retry the request. If it's a random occurrence my guess is a quota limit issue, but the actual error information will likely tell you exactly what you want to know.
MYUSERNAME is not a valid username. Add your own youtube username!
When I call your feed URL in browser (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) I receive this error:
Favorites of requested user are not public.
Make your feed public, and the failure should be gone.

Categories