php cURL 函数 301 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21233771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php cURL function 301 error
提问by user3214712
I am using this code :
我正在使用此代码:
function getUrl($url) {
if(@function_exists('curl_init')) {
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; CrawlBot/1.0.0)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); # required for https urls
curl_setopt($ch, CURLOPT_MAXREDIRS, 15);
$site = curl_exec($ch);
curl_close($ch);
} else {
global $site;
$site = file_get_contents($url);
}
return $site;
};
But i see error 301 Moved Permanently.
但是我看到错误 301 Moved Permanently。
What can i do to solve this ? can you give me the correct code please ?
我能做些什么来解决这个问题?你能给我正确的代码吗?
回答by CONvid19
CURLOPT_FOLLOWLOCATIONcannot be activated when insafe_modeor anopen_basediris set
CURLOPT_FOLLOWLOCATION设置为insafe_mode或 an 时无法激活open_basedir
You may try this:
你可以试试这个:
Solution 1:
解决方案1:
Set safe_mode = Offin your php.inifile (it's usually in /etc/on the server). If that's already off, then look around for the open_basedirstuff in the php.inifile and comment that line (#open_basedir...). Restart apache server.
safe_mode = Off在您的php.ini文件中设置(它通常在/etc/服务器上)。如果这是已经关闭,然后到处寻找open_basedir的东西在php.ini文件和注释该行(#open_basedir ...)。重启apache服务器。
Solution 2:
If the above doesn't work (it should!) try this:
解决方案 2:
如果上述方法不起作用(应该如此!),请尝试以下操作:
<?php
function geturl($url){
(function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; CrawlBot/1.0.0)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); # required for https urls
curl_setopt($ch, CURLOPT_MAXREDIRS, 15);
$html = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
if($status['http_code']!=200){
if($status['http_code'] == 301 || $status['http_code'] == 302) {
list($header) = explode("\r\n\r\n", $html, 2);
$matches = array();
preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
$url = trim(str_replace($matches[1],"",$matches[0]));
$url_parsed = parse_url($url);
return (isset($url_parsed))? geturl($url):'';
}
}
return $html;
}
?>
回答by Mahmoud Adam
You might need to enable follow redirects using the location header flag:
您可能需要使用位置标头标志启用跟随重定向:
curl -L <URL>
curl -L <URL>

