PHP CURLOPT_FOLLOWLOCATION error

Status
Not open for further replies.

projectv3

New member
Mar 6, 2007
506
2
0
Below is the error I've been reciving while trying to run a script on my server:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/domain/public_html/addon_domain/script_folder/includes/functions.php on line 32
In the past this script ran perfectly fine until I had lunarpages upgrade my hosting to PHP5 (in order to run other unaffiliate scripts). I have no actual knowledge in PHP so I'm absolutely clueless.

I've googled this error and it seems to be quite common. Some say its safe_mode and/or open_basedir related, while others said somewhere along PHP4 upgrades, PHP developers crippled the curl_setopt (or was it curlopt_followlocation) due to possible server exploits.

Does anyone have any experience with this? Any possible work arounds? If it matters, the script is the yellowpages.ca scraper that was available from here in the past. It used to provide me with a full list of data but now it only returns an empty xml and cvs file :(

*any help will be very appreciated
 


I haven't seen this error before myself, but you can achieve the same thing as FOLLOWLOCATION manually. If you don't know any PHP this will be pretty tough for you but nonetheless.

First, assign a variable to whether or not FL works:
Code:
$follow_manually = !@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
Then after you get the page
Code:
$page = curl_exec($curl);
, if $follow_manually is true send the page data to another function that strips the headers of any location path. You need CURLOPT_HEADER set to true.
Code:
 preg_match("#Location: ?(.*)#i", $page, $match);
$redirect_url = trim($match[1]);
If you get a URL, put it through the same process otherwise return the page data.

You're going to need to delve into the PHP I'm afraid, but hopefully this should give you an idea of where to start.

Cheers,

Leon
 
Hi,

I've put together a Curl class that should follow correctly for you:
Code:
/*
* Curl class
*/
class curl_class{
    
    function curl_class(){ $this->__construct($array=null); }
    function __construct($array=null) {
    
        /*
        Fill class with default variables
        */    
        if(is_array($array)) {
            foreach($array AS $key=>$value) {
            $this->$key = $value;
            }
        }    
        
        /*
        Specify cookie file
        */    
        if(!$this->cookie){
            $this->cookie = tempnam("tmp","COOK");
        }
        
        /*
        Default user agent
        */            
        if(!$this->user_agent) {            
            $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)';        
        }

        /*
        Init curl
        */        
        $this->curl = curl_init();
        
        $this->configCurl(CURLOPT_SSL_VERIFYPEER, 0);
        $this->configCurl(CURLOPT_SSL_VERIFYHOST, 0);
        $this->configCurl(CURLOPT_RETURNTRANSFER, 1);
        $this->configCurl(CURLOPT_HEADER, 1);
        $this->configCurl(CURLOPT_AUTOREFERER, 1);
        $this->configCurl(CURLOPT_COOKIEFILE, $this->cookie);
        $this->configCurl(CURLOPT_COOKIEJAR, $this->cookie);        
        
        if($this->ajax) {
            $str[]  = "X-Requested-With: XMLHttpRequest";
        }
        
        if($this->prototype) {
            $str[]  = "X-Prototype-Version: ". $this->prototype;
        }        
        
        if($this->prototype || $this->ajax) {        
             $this->configCurl(CURLOPT_HTTPHEADER, $str);
        }
                
        if($this->debug) {
        $GLOBALS['fd'] = fopen($_SERVER['DOCUMENT_ROOT']."/webadmin/log/" . "error.txt", "a+"); 
        $this->configCurl(CURLOPT_VERBOSE, 1);    
        $this->configCurl(CURLOPT_STDERR, $GLOBALS['fd']);    
        }
        
        /*
        Set timeout/
        */            
        $this->setTimeout($this->connect,$this->transfer);


    }

    /*
    Set a curl option
    */    
    function configCurl($option, $value){
        return @curl_setopt ($this->curl, $option, $value);
    }


    /*
    Set curl timeout
    */    
    function setTimeout($connect=20, $transfer=10) {
    
        $this->configCurl(CURLOPT_CONNECTTIMEOUT, $connect);    
        $this->configCurl(CURLOPT_TIMEOUT, $transfer);    


    }

    /*
    Return curl error
    */    
    function get_error_code() {

        return curl_errno($this->curl) ? curl_error($this->curl) : false;

    }


    /*
    Return page http code
    */    
    function get_response_code() {

        return curl_getinfo($this->curl, CURLINFO_HTTP_CODE);

    }


    /*
    HTTP authentication
    */    
    function auth($user, $pass) {

        $this->authinfo = "$user:$pass";        

    }

    /*
    POST function
    */    
    function post($url, $referer = '', $post_string=null) {
    
        //Reset options (but using same cookie)
        $this->__construct();    

        $this->url = $url;
        $this->post_string = $post_string;
                
        //Set curl options
        $this->configCurl(CURLOPT_URL, $this->url);    
        $this->configCurl(CURLOPT_POSTFIELDS, $this->post_string);    
        
        if($this->authinfo) {
            $this->configCurl(CURLOPT_USERPWD,$this->authinfo);                 
        }        
    
        //Set ref
        if($referer){
            $this->configCurl(CURLOPT_REFERER, $referer);    
        }


        //Do we need to follow manually?
        if($this->nofollow) {
            $this->configCurl(CURLOPT_FOLLOWLOCATION, false);            
        } else {        
            $this->follow_manually = !$this->configCurl(CURLOPT_FOLLOWLOCATION, true);                    
        }

        

        $page = curl_exec($this->curl);
        $error = curl_errno($this->curl);    

        if ($error != CURLE_OK || empty($page)) {

            return false;

        }        
        
        if($this->follow_manually) {
        return $this->manual_follow($page);
        } else {
        return $page;        
        }



    }

    /*
    GET function
    */    
    function get($url, $referer = '') {
        
        //Reset options (but using same cookie)
        $this->__construct();

        //Set curl options
        $this->configCurl(CURLOPT_URL, $url);    
        
        if($this->authinfo) {
            $this->configCurl(CURLOPT_USERPWD,$this->authinfo);                 
        }        
    
        //Set ref
        if($referer){
            $this->configCurl(CURLOPT_REFERER, $referer);    
        }


        //Do we need to follow manually?
        if($this->nofollow) {
            $this->configCurl(CURLOPT_FOLLOWLOCATION, false);            
        } else {        
            $this->follow_manually = !$this->configCurl(CURLOPT_FOLLOWLOCATION, true);                    
        }


        $page = curl_exec($this->curl);
        $error = curl_errno($this->curl);    

        if ($error != CURLE_OK || empty($page)) {

            return false;

        }        
        
        if($this->follow_manually) {
        return $this->manual_follow($page);
        } else {
        return $page;        
        }

    }

    /*
    Read headers to follow a location manually
    */        
    function manual_follow($page) {
    
    //Check is follow
    if(substr($this->get_response_code(),0,2) != "30") {
    $this->follow_url = "";
    } else {    
    preg_match("@Location:(.*?)\n@i", $page, $matches);
    $this->follow_url = trim($matches[1]);    
    }
    
    if($this->follow_url) {
    return $this->get($this->follow_url);
    } else {
    return $page;
    }
    
    
    }



};
Some bits borrowed from PHP cUrl Tutorial | OOP Class eHttpClient : 5ubliminal's TellinYa

Cheers,

Leon
 
sweeet, hey thanks alot for this! sorry it took a lil while to reply, my isp has been capping my internet speeds to a point where it takes well over a minute to load a page so i havent really been spending time on forums to focus more time on loading other pages lol

thanks again, very very appreciated!!
 
it's a php4 thing most likely, just encountered that exact same thing last week.

If your host supports php 4 & 5 just add this to your .htaccess file

Code:
AddHandler application/x-httpd-php5 .php

If your host only supports php4 ... tell them to stop living in 2003, or find a new host. All the hacks in the world will not make up for a hosting company that's that out of date.
 
my host supports php5 i only started encountering this error after i made the move to php5+

what does that code to to the htaccess file?
 
Status
Not open for further replies.