• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php curl提示Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated…

php 搞代码 3年前 (2022-01-22) 13次浏览 已收录 0个评论

在我们使用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转向等就会出现文中提到的错误信息.

在查了相关资料后,很快找到了解决办法

具体做法是在curl语句用不使用curl_exec函数

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

自定义一个curl_exec函数

function curlExec(/* Array */$curlOptions='', /* Array */$curlHeaders='', /* Array */$postFields=''){  $newUrl = '';  $maxRedirection = 10;  do  {    if ($maxRedirection<1) die('Error: reached the limit of redirections');    $ch = curl_init();    if (!empty($curlOptions)) curl_setopt_array($ch, $curlOptions);    if (!empty($curlHeaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);    if (!empty($postFields))    {      curl_setopt($ch, CURLOPT_POST, 1);      curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);    }        if (!empty($newUrl)) curl_setopt($ch, CURLOPT_URL, $newUrl); // redirect needed        $curlResult = curl_exec($ch);    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);    if ($code == 301 || $code == 302 || $code == 303 || $code == 307)    {      preg_match('/Location:(.*?)\n/i', $curlResult, $matches);      $newUrl = trim(array_pop($matches));      curl_clos<i>1本文来#源gaodai$ma#com搞$代*码*网</i><pre>搞代gaodaima码

e($ch); $maxRedirection–; continue; } else // no more redirection { $code = 0; curl_close($ch); } } while($code); return $curlResult;}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php curl提示Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated…

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址