Page 1 of 1

conversion to cURL

Posted: Tue Mar 25, 2008 4:09 pm
by bkey
ok here is my problem, i am using an rss feed reader in an external .php file on my server, everything works fine on my test server but my host says that they have allow_url_fopen disabled they are suggesting a cUrl method or otherwise changing my method of linking in the document.

my code is:

Code: Select all

<?php
//
// ScarySoftware RSS parser 
// Copyright (c) 2006 Scary Software
// ( Generates HTML from a RSS feeds )
//    
// Licensed Under the Gnu Public License
//
 
 
class rssReader {
  
var $rssFeeds;
 
// Class constructor
function rssReader() {
 
    // Here are the feeds - you can add to them or change them
    $this->rssFeeds = array(
            0 => "http://url",
            1 => "http://url",
            2 => "http://url",
            3 => "http://url",
            4 => "http://url",
            5 => "http://url",
            6 => "http://url",
            7 => "http://url",
        );
}
 
function checkCache($rss_url) {
 
    $ttl = 60*60;// 60 secs/min for 60 minutes = 1 hour(360 secs)  
    $cachefilename = md5(md5($rss_url));
 
    if (file_exists($cachefilename) && (time() - $ttl < filemtime($cachefilename))) 
    {
        $feed = file_get_contents($cachefilename);
    }
    else
    {
        $feed = file_get_contents($rss_url);
        $fp = fopen($cachefilename, 'w'); 
        fwrite($fp, $feed); 
        fclose($fp); 
    }
 
    return $feed;    
 
}
 
 
 
//
//  Creates HTML from a FeedID in the array
//  it makes $howmany entries
//
function createHtmlFromFeed($feedid, $howmany) {
 
    // Now we make sure that we have a feed selected to work with
    $rss_url = $this->rssFeeds[$feedid];
    if (!isset($rss_url)) $rss_url = $this->rssFeeds[$feedid];
    $howmany = intval($howmany);
 
   $this->createHtmlFromRSSUrl( $rss_url, $howmany );
 
}
 
//
// Create HTML from an RSS URL
// it makes $mowmany entires
//
function createHtmlFromRSSUrl( $rss_url, $howmany )
{
    // Now we get the feed and cache it if necessary
    $rss_feed = $this->checkCache($rss_url);
 
    // Now we replace a few things that may cause problems later
    $rss_feed = str_replace("<![CDATA[", "", $rss_feed);
    $rss_feed = str_replace("]]>", "", $rss_feed);
    $rss_feed = str_replace("\n", "", $rss_feed);
 
   // If there is an image node remove it, we aren't going to use
    // it anyway and it often contains a <title> and <link>
    // that we don't want to match on later.
    $rss_feed = preg_replace('#<image>(.*?)</image>#', '', $rss_feed, 1 );
 
    // Now we get the nodes that we're interested in
    preg_match_all('#<title>(.*?)</title>#', $rss_feed, $title, PREG_SET_ORDER); 
    preg_match_all('#<link>(.*?)</link>#', $rss_feed, $link, PREG_SET_ORDER);
    preg_match_all('#<description>(.*?)</description>#', $rss_feed, $description, PREG_SET_ORDER); 
 
    //
    // Now that the RSS/XML is parsed.. Lets Make HTML !
    //
 
    // If there is not at least one title, then the feed was empty
    // it happens sometimes, so lets be prepared and do something 
    // reasonable
    if(count($title) <= 1)
    {
        echo "No news at present, please check back later.<br><br>";
    }
    else
    {
        // OK Here we go, this is the fun part
 
        // Well do up the top 3 entries from the feed
        for ($counter = 1; $counter <= $howmany; $counter++ )
        {
            // We do a reality check to make sure there is something we can show
            if(!empty($title[$counter][1]))
            {
                // Then we'll make a good faith effort to make the title
                // valid HTML
                $title[$counter][1] = str_replace("&", "&", $title[$counter][1]);
                $title[$counter][1] = str_replace("&apos;", "'", $title[$counter][1]);     
                $title[$counter][1] = str_replace("&pound;", "?", $title[$counter][1]);     
 
                // The description often has encoded HTML entities in it, and
                // we probably don't want these, so we'll decode them
                $description[$counter][1] =  html_entity_decode( $description[$counter][1]);     
 
                // Now we make a pretty page bit from the data we retrieved from
                // the RSS feed.  Remember the function FormatRow from the 
                // beginning of the program ?  Here we put it to use.
                $row = $this->FormatEntry($title[$counter][1],$description[$counter][1],$link[$counter][1]);
 
                // And now we'll output the new page bit!
                echo $row;
            }
        }
    }
}
 
function FormatEntry($title, $description, $link) {
return <<<eof
 
<!-- RSS FEED ENTRY -->
<p class="feed_title">{$title}</p>
<p class="feed_description">{$description}</p>
<a class="feed_link" href="{$link}" rel="nofollow" target="_blank">Read more...</a>
<p>&nbsp;</p>
<hr size=1>
<!-- END OF RSS FEED ENTRY -->
 
 
eof;
}
 
 
function GetrssFeeds() {
   return $this->rssFeeds;
}
 
function SetrssFeeds($rssFeeds) {
   $this->rssFeeds = $rssFeeds;
}
 
 
}// END OF THE CLASS
 
?>
i cant figure out howto make it work either changing the link parameters to something like $_SERVER['DOCUMENT_ROOT']."url_to_feed"

or changing to cURL. Any help here would be appreciated i really dont know much about php.

Re: conversion to cURL

Posted: Tue Mar 25, 2008 4:48 pm
by help-php
Some info about CURL is here: http://ca.php.net/manual/en/ref.curl.php.

I think you need to replace your file_get_contents code with using CURL:

Original part:
----
$feed = file_get_contents($rss_url);
$fp = fopen($cachefilename, 'w');
fwrite($fp, $feed);
fclose($fp);
----

New part:
----
$ch = curl_init($rss_url);
$fp = fopen($cachefilename, 'w');

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
----

Re: conversion to cURL

Posted: Tue Mar 25, 2008 4:57 pm
by bkey
:banghead:

i tried that, it does the exact same thing.

the probloem is when it tries to go out and get the feed from the external url, allow_url_fopen being disabled, it doesnt work, when i make the changes you suggest, again it works on my test server but not on the live server.

someone also mentioned that i could rewrite the urls so that it grabbed from the sub domains as its supposed to but when i try that i get an error telling me that there is no such directory at line 42 of that .php file

the php file shown is hosted on my main domain, the files that i am trying to access are subdomains of the main
ie:
mydomain.com >> contains .php script
show.mydomain.com >> contains teh rss feed i am trying to get.