我遇到了一个错误
当启用safe_mode或设置open_basedir时,无法激活CURLOPT_FOLLOWLOCATION
我在Google上找到了很多解决方案,但对于这个网站它们都不起作用。只需要CURLOPT_FOLLOWLOCATION。愚蠢的主机商不想启用safe_mode或open_basedir。我能否自己启用它们,也许创建一个带有一些参数的htaccess文件?
这个错误意味着safe_mode或open_basedir被启用了(可能是open_basedir),如果您的主机有任何良好的安全设置,则可能无法覆盖它们。
所以你有两种选择:
1)更换主机(我想这不是一个理想的选择)
2)使用类似于在php curl_setopt页面中找到的函数,例如http://www.php.net/manual/en/function.curl-setopt.php#95027
以下是第二点中指定函数的修复版本。
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;
}
}
CURLOPT_REDIR_PROTOCOLS
指定了只允许重定向使用 http 和 https 协议,那么您就可以在不使用 safe_mode
或 open_basedir
的情况下使用 CURLOPT_FOLLOWLOCATION
。CURLOPT_REDIR_PROTOCOLS
设置为CURLPROTO_HTTP | CURLPROTO_HTTPS
似乎无法解决open_basedir
的问题。 - Mahn你好,我修复了添加这个条件:
$safeMode = @ini_get('safe_mode');
$openBasedir = @ini_get('open_basedir');
if (empty($safeMode) && empty($openBasedir)) {
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
}else
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
并在我的 .htaccess 文件中添加
php_flag safe_mode off
php_flag open_basedir off
[^\n]
。在递归调用curl_redirect_exec
时,您应该传递$curlopt_header
。 - Kendall Hopkins$xml = curl_redirect_exec($ch, true);
。 - celliott1997