CURLOPT_FOLLOWLOCATION出了什么问题?

时间:2011-02-07 18:54:45

标签: php

我在尝试将文件上传到亚马逊s3时遇到此问题,它给了我这个错误,但我似乎明白了:

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

5 个答案:

答案 0 :(得分:2)

在curl函数的评论中发布了一个冗长的解决方法:
http://php.net/manual/en/function.curl-setopt.php#102121

虽然更好的解决方案是不使用cURL。 (有关替代方案,请参阅PEAR Http_Request2或Zend_Http,如果可用,请使用内置的HttpRequest。)

答案 1 :(得分:0)

问题正是错误消息中的内容 - 您在php.ini中启用了safe_mode或open_basedir。编辑php.ini来禁用你所拥有的那些,或者不使用PHP的curl风格。如果您无法编辑php.ini,则必须找到新的主机或找到新的解决方案。

答案 2 :(得分:0)

最好的解决方案是获得一个新的主机。 open_basedir不是一个很好的安全功能(一个好的主机将使用更好的方法设置一个监狱)。不推荐使用safe_mode。因此,最好的结果将来自禁用这两个指令(或者如果您的指令不愿意,可以找到一个新的主机)。

但是,如果这不是一个选项,你总是可以实现this之类的东西(来自php.net上的评论)......

答案 3 :(得分:0)

我有马里奥发布的更短且更不安全的变通方法,但您可能会发现它对于具有已知重定向数量的网址非常有用(例如FB Graph API图像调用 - graph.facebook.com/4/picture)< / p>

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;
    }
}

答案 4 :(得分:0)

使用此版本的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";}