home | contact us
» curl » PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode

BLOG CATEGORY: curl


If you are trying to get a curl script which needs follow on location functionality to run on a server which has either open_basedir or safe mode enabled you will get an error message similar to the following:

CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set

After a bit of digging, some kind soul has put a workaround here

Here is how to use the function


function curl($url){
$go = curl_init($url);
curl_setopt ($go, CURLOPT_URL, $url);
//follow on location problems

if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){

curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);

$syn = curl_exec($go);

}else{

$syn = curl_redir_exec($go);

}

curl_close($go);
return $syn;
}

//follow on location problems workaround

function curl_redir_exec($ch)

{

static $curl_loops = 0;

static $curl_max_loops = 20;

if ($curl_loops++ >= $curl_max_loops)

{

$curl_loops = 0;

return FALSE;

}

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);

list($header, $data) = explode("\n\n", $data, 2);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code == 301 || $http_code == 302)

{

$matches = array();

preg_match('/Location:(.*?)\n/', $header, $matches);

$url = @parse_url(trim(array_pop($matches)));

if (!$url)

{

//couldn't process the url to redirect to

$curl_loops = 0;

return $data;

}

$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));

if (!$url['scheme'])

$url['scheme'] = $last_url['scheme'];

if (!$url['host'])

$url['host'] = $last_url['host'];

if (!$url['path'])

$url['path'] = $last_url['path'];

$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');

curl_setopt($ch, CURLOPT_URL, $new_url);

return curl_redir_exec($ch);

} else {

$curl_loops=0;

return $data;

}

}


Click Here to Contact Us about PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode
 

Comments

32 Comments

32 Responses to “PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode”

  1. sakao says:

    this is not a working code
    as far as i can see
    there’s no
    $go = curl_init($url);
    in the first line

  2. admin says:

    oops sorry about that- i have add this in there.

  3. Karl says:

    Very nice, works like a treat. Am using it with domdocument and xpath to scrape content from various sources.

    Quick tip: obvious, but for php novices, you’ll need to add something like the following to your code to actually start the process

    $html=curl(‘http://www.urltobeparsed.com’);

    See here for more info: http://www.merchantos.com/makebeta/php/scraping-links-with-php/

  4. Ali says:

    Hello,

    I am getting the error

    CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set

    What can be the possible solution. I didnt understand the above script you have written

  5. nd says:

    Nice code!

    However, I had a problem with it since
    explode(“\n\n”, $data, 2);

    does not work on all platforms. Correct is
    explode(“\n\r”, $data, 2);

    Regards,
    Andy

  6. admin says:

    nice one Andy.

    I wonder if we could mod the function to work on any platform..

    would swapping \n\n for PHP_EOL work?

  7. steve says:

    nd – you’re right about the \n\r this worked for me.

    also, error in line 6, should be:

    if (ini_get(‘open_basedir’) == ” && ini_get(‘safe_mode’) == ‘Off’){

    in my case I also had to change the safe mode check because safe mode off is an empty string:

    if (ini_get(‘open_basedir’) == ” && ini_get(‘safe_mode’ == ”)){

  8. Andy says:

    Wonderful, saved my life :) I used it on an connector which i wrote for plugging WordPress and CakePHP together. In this constellation, there was no $url["query"] and i got an error message. So this is my solution for that:

    $new_url = $url['scheme'].’://’.$url['host'].$url['path'].(array_key_exists(‘query’, $url) && $url['query'] ? ‘?’.$url['query'] : ”);

    Maybe it´s usefull for someone.

    Nice greetings and a big thank you for this workaround.

    Andy

  9. Funny Images says:

    Yes this is working on my site.

    Thanks for solution

  10. tim says:

    What do you called the script before uploading it to the server

  11. Mesut says:

    Great job thanks. You helped me to fix this very annoying problem. Thanks very much.

  12. Ron says:

    You are a god! ;)

    Took me 2 hours of configuring but now it works! Thanks alot! :)

  13. budflick says:

    thanks! worked like a charm

  14. Daniel says:

    This saved me A LOT of time. Thank you!

  15. Karthik says:

    Awesome script. Really helped me!

    Thanks,
    Karthik

  16. Sand says:

    Error on Line 94 – Notice: Undefined offset: 1:
    list($header, $data) = explode(“\n\n”, $data, 2);

  17. Sand says:

    list($header, $data) = explode(“\n\n”, $data, 2);

    $header == ??

  18. Andrew says:

    Sand, $header is the first element of the $data array, but you may need to change “\n\n” to something which works on your system. See earlier comments.

  19. wawan says:

    i have a website using wp.
    when i want to install the plugin, i got the alert

    Warning: curl_setopt_array() [function.curl-setopt-array]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/greendi4/public_html/kindle/wp-content/plugins/instant_traffic_robot/includes/classes/plugin.class.php on line 847

    can you help me with this problem??

  20. Andreas says:

    Hi @ all,

    i’m using the script on both windows an linux platforms.
    Therefore the lines that explodes the header have to be changed.

    Insteaf of:
    $data = curl_exec($ch);
    list($header, $data) = explode(“\n\n”, $data, 2);

    use this:
    $data1 = curl_exec($ch);
    //Linux
    list($header, $data) = explode(“\n\n”, $data1, 2);
    if($data==null) {
    //Windows-System
    list($header, $data) = explode(“\n\r”, $data1, 2);
    }

    Best regards

    Andreas

  21. Tony Huang says:

    curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);

    What is the $l?

  22. Glenn Robert says:

    I just wanna say thanks to the author and all the contributors via comments! This code worked like a charm for me!
    Cheers:-)

  23. Glenn Robert says:

    To Tony Huang: The $l you can just delete and enter ’1′ instead. This means that if safe mode is not set, curl will make use of the CURLOPT_FOLLOWLOCATION. Anyhow, if CURLOPT_FOLLOWLOCATION seems to work for you, you are in no need of this script..

  24. idham says:

    Thanks, may i can use you code to fix my problem as you mention.

  25. Jason Muller says:

    This post helped another one with the safe_mode/open_basedir problem and I am posting this comment to say thank you, finally my problem is solved.

  26. This saved me A LOT of time. Thank you!

  27. Bob says:

    Hi,

    I am a complete newbie here so sorry in advance;

    I am getting the error;
    =======================
    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/inkingst/public_html/install.php on line 25
    [2012-04-05 16:01:17] Unable to login to cpanel
    =======================

    On line 25 of install.php it says;
    =======================
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    =======================

    Where should I put the workaround code? Again I am sorry but I don’t have a clue :(

    Thanks in advance.

    Bob.

  28. Hi, Nice work – but I god this problem at my WP-blog – can You help?

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /var/www/danish-artinfo.dk/public_html/wp-content/plugins/wp-portfolio/lib/
    thumbnailer.inc.php on line 552

    Regards Jesper

  29. Hi,
    Please anyone?

    Need som newbie help :-)

  30. Michelle says:

    Hi,

    Yes, my problem has been solved with this code.

  31. John says:

    Hi, this seems to be the solution to my problem but I’m not sure. I’m getting the following errors and would like to know if the code above would be a solution for me. If so, please tell me what or how I have to upload it to the server. Thank you so much…

Leave a Reply

rss icon