php curl与CURLOPT_FOLLOWLOCATION错误

kus*_*agi 10 php curl

我收到了一个错误

启用safe_mode或设置open_basedir时,无法激活CURLOPT_FOLLOWLOCATION

我谷歌很多解决方案,但与这个网站,他们不工作.只需要CURLOPT_FOLLOWLOCATION.stupid hoster不想启用safe_mode或open_basedir.我可以自己启用它们,可以用一些参数创建htaccess吗?

Rwk*_*wky 13

该错误意味着启用了safe_mode或open_basedir(可能是open_basedir),如果你的主机有任何不错的安全设置,你可能无法覆盖.

所以你有一个选择

1)改变主机(我想象的并不是真的可取)

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

以下是第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;
    }
}
Run Code Online (Sandbox Code Playgroud)