Modifying lastRSS to use CURL Instead of fopen()

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.

Note: 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.

Author avatar

About the author

New Hampshirite building web apps in Florida. Creator of Surreal CMS, Postleaf, and DirtyMarkup.

Need to get in touch? Catch me on Twitter.