教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php CURLOPT错误Warning: curl_setopt() [function.curl-setopt]:...

php CURLOPT错误Warning: curl_setopt() [function.curl-setopt]:...

发布时间:2018-09-20   编辑:jiaochengji.com
教程集为您提供php CURLOPT错误Warning: curl,setopt() [function.curl-setopt]:...等资源,欢迎您收藏本站,我们将为您提供最新的php CURLOPT错误Warning: curl,setopt() [function.curl-setopt]:...资源
在我们使用php curl函数时提示Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…错误,下面我就来介绍碰到此问题要如何来排除问题吧。

如果当你在php中运行 CURLOPT_FOLLOWLOCATION 然后得到php提示错误信息为:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

错误中提到两个关键safe_mode和 open_basedir,如果你是虚拟主机的没有设置APPCHE的权限是不能通过修改服务器配置来解决问题的,一般来说,服务器配置safe_mode都为off,然后为了一些安全对用户有一些限制,通过设置open_basedir来限制虚拟主机用户的PHP执行文件夹,因此当你使用CURLOPT_FOLLOWLOCATION (php curl函数,深层抓取数据)的时候,一旦有301转向等就会出现文中提到的错误信息.

在查了相关资料后,很快找到了解决办法,http://www.php.net/manual/en/function.curl-setopt.php,这些方法都在php官方帮助里有.

具体做法是在curl语句用不使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true),在php函数中自定义一个函数,

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9855')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9855>
function curl_redir_exec($ch,$debug="")
{
static $curl_loops = 0;
static $curl_max_loops = 20;
if ($curl_loops >= $curl_max_loops)
{
$curl_loops = 0;
return FALSE;
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$debbbb = $data;
list($header, $data) = explode("\n\n", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = @parse_url(trim(array_pop($matches)));
//print_r($url);
if (!$url)
{
//couldn't process the url to redirect to
$curl_loops = 0;
return $data;
}
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
/*    if (!$url['scheme'])
$url['scheme'] = $last_url['scheme'];
if (!$url['host'])
$url['host'] = $last_url['host'];
if (!$url['path'])
$url['path'] = $last_url['path'];*/
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
curl_setopt($ch, CURLOPT_URL, $new_url);
//    debug('Redirecting to', $new_url);
return curl_redir_exec($ch);
} else {
$curl_loops=0;
return $debbbb;
}
}

函数定义好后,curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)这条语句替换为curl_redir_exec($ch)

这样以后,我想你的PHP文件应该不会提示错误了,关于这段代码,在提供PHP官方连接用可以找到.

您可能感兴趣的文章:
php CURLOPT错误Warning: curl_setopt() [function.curl-setopt]:...
php抓取https内容的方法
php curl应用实例分析
php curl模块的用法举例
php file_get_contents抓取https地址报错的解决办法
PHP实现抓取HTTPS内容的方法和错误处理
PHP curl 获取响应的状态码实例详解
php curl调用接口显示HTTP Status 415如何解决
file_get_contents无法请求https连接的解决方法
关于PHP的curl功能扩展基本用法

[关闭]
~ ~