curl跟随位置错误 [英] curl follow location error
本文介绍了curl跟随位置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误讯息:
CURLOPT_FOLLOWLOCATION无法在safe_mode或open_basedir设定时启用。
safe_mode在我的网络托管中已关闭。
open_basedir是。 >
如何解决此问题?
解决方案
解决方法是实现重定向PHP代码。
这是我自己的实现。它有两个已知的限制:
- 它会强制
CURLOPT_RETURNTRANSFER
- 与
CURLOPT_HEADERFUNCTION
不兼容
function curl_exec_follow(/ * resource * /& $ ch,/ * int * / $ redirects = 20,/ * bool * / $ curlopt_header = false){
if((!ini_get('open_basedir')&&!ini_get('safe_mode'))|| $ redirects< 1){
curl_setopt ch,CURLOPT_HEADER,$ curlopt_header);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,$ redirects> 0);
curl_setopt($ ch,CURLOPT_MAXREDIRS,$ redirects);
return curl_exec($ ch);
} else {
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($ ch,CURLOPT_HEADER,true);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FORBID_REUSE,false);
do {
$ data = curl_exec($ ch);
if(curl_errno($ ch))
break;
$ code = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
if($ code!= 301&& $ code!= 302)
break;
$ header_start = strpos($ data,\r\\\
)+ 2;
$ headers = substr($ data,$ header_start,strpos($ data,\r\\\
\r\\\
,$ header_start)+ 2- $ header_start);
if(!preg_match(!\r\\\
(?:Location | URI):*(。*?)* \r\\\
!,$ headers,$ matches))
break;
curl_setopt($ ch,CURLOPT_URL,$ matches [1]);
} while( - $ redirects);
if(!$ redirects)
trigger_error('Too many redirects。When following redirects,libcurl hit the maximum amount。',E_USER_WARNING);
if(!$ curlopt_header)
$ data = substr($ data,strpos($ data,\r\\\
\r\\\
)+ 4);
return $ data;
}
}
I got this error message:
CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in.
safe_mode is turned off on my web hosting.
open_basedir is "".
How do I resolve this?
解决方案
The workaround is to implement redirection in PHP code.
Here is my own implementation. It has two known limitations:
- It will force
CURLOPT_RETURNTRANSFER
- It is incompatible with
CURLOPT_HEADERFUNCTION
The code:
function curl_exec_follow(/*resource*/ &$ch, /*int*/ $redirects = 20, /*bool*/ $curlopt_header = false) {
if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
curl_setopt($ch, CURLOPT_HEADER, $curlopt_header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects);
return curl_exec($ch);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);
do {
$data = curl_exec($ch);
if (curl_errno($ch))
break;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 301 && $code != 302)
break;
$header_start = strpos($data, "\r\n")+2;
$headers = substr($data, $header_start, strpos($data, "\r\n\r\n", $header_start)+2-$header_start);
if (!preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!", $headers, $matches))
break;
curl_setopt($ch, CURLOPT_URL, $matches[1]);
} while (--$redirects);
if (!$redirects)
trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
if (!$curlopt_header)
$data = substr($data, strpos($data, "\r\n\r\n")+4);
return $data;
}
}
这篇关于curl跟随位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文