使用phpcurl函数时提示Warning:curl_setopt()[function.curl-setopt]:CURLO…错误该怎么解决
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用phpcurl函数时提示Warning:curl_setopt()[function.curl-setopt]:CURLO…错误该怎么解决,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3573字,纯文字阅读大概需要6分钟。
内容图文
![使用phpcurl函数时提示Warning:curl_setopt()[function.curl-setopt]:CURLO…错误该怎么解决](/upload/InfoBanner/zyjiaocheng/146/cb0504689c5b4893aefb14acc36a16c8.jpg)
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.gxlcms.com/php-weizijiaocheng-362563.html,这些方法都在php官方帮助里有.
具体做法是在curl语句用不使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true),在php函数中自定义一个函数,
代码如下:
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官方的代码如下
/safe 模式不能curl的函数 function curl_redir_exec($ch) { 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); 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))); 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 $data; } } //end
【相关文章推荐】
1. php curl_setopt函数概念与用法实例介绍
2.使用php curl_setopt()函数实现抓取网页与POST数据的简单例子
3.php curl_setopt函数模拟用户登录示例
4.PHP curl_exec函数的用法实例详解
以上就是使用php curl函数时提示Warning: curl_setopt() [function.curl-setopt]: CURLO…错误该怎么解决的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的使用phpcurl函数时提示Warning:curl_setopt()[function.curl-setopt]:CURLO…错误该怎么解决全部内容,希望文章能够帮你解决使用phpcurl函数时提示Warning:curl_setopt()[function.curl-setopt]:CURLO…错误该怎么解决所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。