带有 CURLOPT_FOLLOWLOCATION 错误的 php curl [英] php curl with CURLOPT_FOLLOWLOCATION error
问题描述
我有一个错误
CURLOPT_FOLLOWLOCATION 在启用 safe_mode 或设置了 open_basedir 时无法激活
CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in
我用谷歌搜索了很多解决方案,但在这个网站上它们不起作用.只需要 CURLOPT_FOLLOWLOCATION.愚蠢的主机不想启用 safe_mode 或 open_basedir .我可以自己启用它们吗,可以用一些参数创建 htaccess 吗?
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?
推荐答案
该错误意味着启用了 safe_mode 或 open_basedir ARE(可能是 open_basedir),如果您的主机具有任何合适的安全设置,您可能无法覆盖.
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.
所以你有选择
1) 更换主机(我想象中不太理想)
1) change host (not really desirable I imagine)
2) 使用类似于 php curl_setopt 页面上的函数,即 http://www.php.net/manual/en/function.curl-setopt.php#95027
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
以下是第2点指定函数的固定版本
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("
", $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("
", $data, 2);
return $body;
}
}
这篇关于带有 CURLOPT_FOLLOWLOCATION 错误的 php curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!