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

如果当你在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函数中自定义一个函数,

 代码如下 复制代码

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的问题是在使用 PHPCURL 发送 POST 请求生成微信小程序码时,出现了“data format error”的错误提示。经过你的调试发现,问题出在 CURLOPT_POSTFIELDS 参数上。 首先,确保你的 $access_token 是有效的,并且请求的地址是正确的。其次,根据微信官方文档,getwxacodeunlimit 接口需要传递的参数中,scene 不能为空,page 可以为空,width 默认为 430,也可以自定义。 在你的代码中,$fields 变量中的参数看起来没有问题,但是由于没有设置 CURLOPT_HTTPHEADER 参数,导致 CURL 默认将 POST 数据以 application/x-www-form-urlencoded 格式发送。而微信官方要求,getwxacodeunlimit 接口需要的 POST 数据是以 raw 格式发送的,也就是需要设置请求头: ```php curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); ``` 这样修改后的代码如下: ```php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); $fields = [ 'scene' => $data['scene'], 'page' => $data['page'], 'width' => 500 ]; curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $res = curl_exec($curl); curl_close($curl); var_dump(json_encode($res)); ``` 希望这能够解决你的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值