pingpong
pingpong

Reputation: 1247

something went wrong with CURLOPT_FOLLOWLOCATION?

i have this problem when im trying to upload a file to amazon s3, it gives me this error but i dnt seem to understand:

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/vhosts/??????/httpdocs/actions/S3.php on line 1257

Upvotes: 1

Views: 5047

Answers (5)

T.Todua
T.Todua

Reputation: 56497

Use this version of Curl

//=================== compressed version===============(https://github.com/tazotodua/useful-php-scripts/)
function get_remote_data($url, $post_paramtrs=false)    {   $c = curl_init();curl_setopt($c, CURLOPT_URL, $url);curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); if($post_paramtrs){curl_setopt($c, CURLOPT_POST,TRUE);  curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&".$post_paramtrs );}  curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0"); curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;'); curl_setopt($c, CURLOPT_MAXREDIRS, 10);  $follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true;  if ($follow_allowed){curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);}curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);curl_setopt($c, CURLOPT_REFERER, $url);curl_setopt($c, CURLOPT_TIMEOUT, 60);curl_setopt($c, CURLOPT_AUTOREFERER, true);         curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');$data=curl_exec($c);$status=curl_getinfo($c);curl_close($c);preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si',  $status['url'],$link);$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si','$1=$2'.$link[0].'$3$4$5', $data);$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si','$1=$2'.$link[1].'://'.$link[3].'$3$4$5', $data);if($status['http_code']==200) {return $data;} elseif($status['http_code']==301 || $status['http_code']==302) { if (!$follow_allowed){if(empty($redirURL)){if(!empty($status['redirect_url'])){$redirURL=$status['redirect_url'];}}   if(empty($redirURL)){preg_match('/(Location:|URI:)(.*?)(\r|\n)/si', $data, $m);if (!empty($m[2])){ $redirURL=$m[2]; } } if(empty($redirURL)){preg_match('/href\=\"(.*?)\"(.*?)here\<\/a\>/si',$data,$m); if (!empty($m[1])){ $redirURL=$m[1]; } }   if(!empty($redirURL)){$t=debug_backtrace(); return call_user_func( $t[0]["function"], trim($redirURL), $post_paramtrs);}}} return "ERRORCODE22 with $url!!<br/>Last status codes<b/>:".json_encode($status)."<br/><br/>Last data got<br/>:$data";}

Upvotes: 0

Marek Jalovec
Marek Jalovec

Reputation: 1427

i have shorter and less safe variant of workaround posted by mario, but you may find it useful for urls with known number of redirects (for example FB Graph API image calls -- graph.facebook.com/4/picture)

function cURLRequest($url) {
    $ch = curl_init();
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);

    if ($result) {
        curl_close($ch);
        return $result;
    } else if (empty($result)) {
        $info = curl_getinfo($ch);
        curl_close($ch);
        // PHP safe mode fallback for 302 redirect
        if (!empty($info['http_code']) && !empty($info['redirect_url'])) {
            return cURLRequest($info['redirect_url']);
        } else {
            return null;
        }
    } else {
        return null;
    }
}

Upvotes: 0

mario
mario

Reputation: 145492

There is a lengthy workaround posted in the comments to the curl functions:
http://php.net/manual/en/function.curl-setopt.php#102121

Though the better solution would be not to use cURL. (See PEAR Http_Request2 or Zend_Http for alternatives, or use PHPs built-in HttpRequest if available.)

Upvotes: 2

ircmaxell
ircmaxell

Reputation: 165261

The best solution would be to get a new host. open_basedir isn't a great security feature (a good host will use the far better approach of setting up a jail). safe_mode is deprecated. So the best result will come from disabling both directives (or finding a new host if yours is unwilling to do so).

However, if that's not an option, you can always implement something like this (from a comment on php.net)...

Upvotes: 0

schizodactyl
schizodactyl

Reputation: 1455

The problem is exactly what is says in the error message - you have safe_mode or open_basedir enabled in php.ini. Either edit php.ini to disable whichever one of those you have on, or don't use PHP's flavor of curl. If you can't edit php.ini you'll have to find a new host or find a new solution.

Upvotes: 0

Related Questions