php cURL函数301错误 [英] php cURL function 301 error
问题描述
我使用这个代码:
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;
};
但我看到错误301永久移动。
<我可以做什么来解决这个?
<$
safe_mode
或
open_basedir
CURLOPT_FOLLOWLOCATION >
您可以尝试:
解决方案1:
在
中设定
safe_mode = Off
/ code>文件(通常在服务器上的 / etc /
中)。如果这已经关闭,那么请查看 php.ini
文件中的 open_basedir
open_basedir ...)。重新启动apache服务器。
解决方案2:
如果上述操作不起作用:
<?php
function geturl($ url){
(function_exists ('curl_init'))? '':die('cURL必须安装为geturl函数工作。请求您的主机启用它或取消注释extension = php_curl.dll在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\\\
\r\\\
,$ html,2);
$ matches = array();
preg_match(/(Location:| URI:)[^(\\\
)] * /,$ header,$ matches);
$ url = trim(str_replace($ matches [1],,$ matches [0]));
$ url_parsed = parse_url($ url);
return(isset($ url_parsed))? geturl($ url):'';
}
}
return $ html;
}
?>
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.
What can i do to solve this ? can you give me the correct code please ?
CURLOPT_FOLLOWLOCATION
cannot be activated when insafe_mode
or anopen_basedir
is set
You may try this:
Solution 1:
Set safe_mode = Off
in your php.ini
file (it's usually in /etc/
on the server). If that's already off, then look around for the open_basedir
stuff in the php.ini
file and comment that line (#open_basedir...). Restart apache server.
Solution 2:
If the above doesn't work (it should!) try this:
<?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;
}
?>
这篇关于php cURL函数301错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!