php curl与CURLOPT_FOLLOWLOCATION错误 [英] php curl with CURLOPT_FOLLOWLOCATION error

查看:142
本文介绍了php curl与CURLOPT_FOLLOWLOCATION错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误


在启用safe_mode或在 $ b $中设置open_basedir时,无法激活CURLOPT_FOLLOWLOCATION b

我google的解决方案,但是用这个网站,他们不工作。只需要CURLOPT_FOLLOWLOCATION。愚蠢的主机不想启用safe_mode或open_basedir。

解决方案

错误意味着safe_mode或open_basedir启用了open_basedir)你可能不能重写,如果你的主机有任何体面的安全设置。



所以你有一个选择



1)更改主机

2)使用类似于在php curl_setopt页面上找到的函数,即 http://www.php.net/manual/en/function.curl-setopt.php#95027



以下是第2点中指定的函数的固定版本

 函数curl_redirect_exec($ ch,& $重定向,$ curlopt_header = false){
curl_setopt($ ch,CURLOPT_HEADER,true);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);

$ data = curl_exec($ ch);

$ http_code = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
if($ http_code == 301 || $ http_code == 302){
list($ header)= explode(\r\\\
\r\\\
,$ data, 2)。

$ matches = array();
preg_match(/(Location:| URI:)[^(\\\
)] * /,$ header,$ matches);
$ url = trim(str_replace($ matches [1],,$ matches [0]));

$ url_parsed = parse_url($ url);
if(isset($ url_parsed)){
curl_setopt($ ch,CURLOPT_URL,$ url);
$ redirects ++;
return curl_redirect_exec($ ch,$ redirects,$ curlopt_header);
}
}

if($ curlopt_header){
return $ data;
} else {
list(,$ body)= explode(\r\\\
\r\\\
,$ data,2);
return $ body;
}
}


i got an error

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

i google many solution, but with this site they don't work. only need CURLOPT_FOLLOWLOCATION. stupid hoster don't want to enable safe_mode or an open_basedir . can i enable them by myself, may be create htaccess with some parameters?

解决方案

The error means safe_mode or open_basedir ARE enabled (probably open_basedir) you probably can't override either if your host has any decent security settings.

So you have a choice

1) change host (not really desirable I imagine)

2) use a function similar to ones found on the php curl_setopt page, i.e. http://www.php.net/manual/en/function.curl-setopt.php#95027

The following is a fixed version of the function specified in point 2

function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($ch);

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code == 301 || $http_code == 302) {
        list($header) = explode("\r\n\r\n", $data, 2);

        $matches = array();
        preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
        $url = trim(str_replace($matches[1], "", $matches[0]));

        $url_parsed = parse_url($url);
        if (isset($url_parsed)) {
            curl_setopt($ch, CURLOPT_URL, $url);
            $redirects++;
            return curl_redirect_exec($ch, $redirects, $curlopt_header);
        }
    }

    if ($curlopt_header) {
        return $data;
    } else {
        list(, $body) = explode("\r\n\r\n", $data, 2);
        return $body;
    }
}

这篇关于php curl与CURLOPT_FOLLOWLOCATION错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆