錯誤意味着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;
}
}
有趣,但腳本在我的主機上運行良好(廉價和虛擬),但老闆使用愚蠢的主機,並沒有改變的願望。關於這個功能 - 我知道它,當我開始寫腳本我手動調用每個url,但最後一個url不會不會工作,只有使用CURLOPT_FOLLOWLOCATION幫助我 – kusanagi 2010-10-08 13:25:37
Eek對不起,我沒有實際測試函數,重新對它不起作用(它假設位置標題不是最後一個)我已經添加了一個我測試過的修正版本給我的答案。 – Rwky 2010-10-08 17:31:08
您的解決方案有一些錯誤。正則表達式應該是'[^ \ n]'。當遞歸調用'curl_redirect_exec'時,你應該傳遞'$ curlopt_header'。 – 2011-11-01 22:16:15