Modifying lastRSS to use CURL Instead of fopen()

A note from A Beautiful Site’s founder: We developed Surreal to be easy for you and your clients. If you’re a web designer, you should take a look at the simple, hosted CMS that’s changing the way content is managed. Surreal integrates in moments and is trusted by over 18,000 websites. Try it out for free and let us know what you think! Visit Website »

If you are using the lastRSS PHP class to parse RSS files from external websites, you may run into a problem like this one:

Warning: fopen(): URL file-access is disabled in the server configuration

This means that your version of PHP isn’t configured to support the URL fopen wrapper. In other words, you can’t use fopen() to access HTTP:// addresses. This can be detrimental if you don’t have access to your PHP config, as is the case with many webhosts.

Fortunately, there is a way to work around this with the PHP CURL library (libcurl).

Modifying lastRSS to Use CURL

Note that the version of lastRSS I am working with is 0.9.1. Future versions may be slightly (or drastically) different, but the concept should remain the same: modify the section of code that retrieves the RSS feed so that instead of using fopen() we’re using CURL.

The lines that need to be modified are around line 135 in version 0.9.1:

if ($f = fopen($rss_url, 'r')) {
    $rss_content = '';
    while (!feof($f)) {
        $rss_content .= fgets($f, 4096);
    }

    fclose($f);

Replace the code with this:

// CURL mod
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rss_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rss_content = curl_exec($ch);
curl_close($ch);

if( $rss_content ) {
// End CURL mod

Now, providing you have the CURL library (libcurl) installed lastRSS will work for just about any URL.

I need to improve the error checking to detect 404 Page Not Found errors. Currently, if a 404 error is reached the HTML is returned to $rss_content (which usually results in nothing after it goes through the parser). If anyone gets to this before I do please send the updated code so I can get it posted.

If you enjoyed this article, please share it with a friend!

3 Responses to Modifying lastRSS to use CURL Instead of fopen()

  1. Julien says:

    You are a life savior …

    Thanks a bunch for that one, you should get that patch included in lastRSS, remote fopen are bad, more and more provider will block that function and allow only curl.

  2. apis says:

    All this while i just abandoned the function because its useless on my hosting but thanks for the useful guide, i am able to make the news function work perfectly now :)

  3. Ives says:

    WOW that was awesome. Thanks for that. Some hosts are just stupid

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>