小程序获取手机号使用php和curlpost

开始

小程序端代码:

<view><button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">授权手机号</button></view>
getPhoneNumber (e) {
    console.log("getPhoneNumber :: ");
    console.log(e);
    console.log(e.detail.code);
    wx.request({ //获取用户的唯一openId
      url: 'http://app.tjb.com/wechat/user/getPhoneNumber',
      data: {
        code: e.detail.code
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.res;
      }
    });

  }

PHP端代码:

/**
     * 获取手机号
     * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
     * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
     * @return bool|string
     */
    function getPhoneNumber(){
        $code = input('code');
        Log::error("1 getPhoneNumber code:".$code);
        Log::error("1 getPhoneNumber post:".json_encode($_POST).', GET:'.json_encode($_GET));
        //1 获取 ACCESS_TOKEN https://api.weixin.qq.com/cgi-bin/token
        $url                    = 'https://api.weixin.qq.com/cgi-bin/token?appid='.$this->appId.'&secret='.$this->AppSecret.'&js_code='.$code.'&grant_type=client_credential';
        $curl_return_data       = Curls($url);
        $curl_return_ary       =@json_decode($curl_return_data, true);
        if(!$curl_return_ary || empty($curl_return_ary['access_token']))
        {
            Log::error("ACCESS_TOKEN获取失败".$url);
            return $this->error('ACCESS_TOKEN获取失败');
        }
        $access_token           = $curl_return_ary['access_token'];

        //https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
        $url                    = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token;
        $curl_return_data      = postCurl($url, ['code'=>$code], true);
        $curl_return_ary       =@json_decode($curl_return_data, true);
        if(!$curl_return_ary || empty($curl_return_ary['phone_info']))
        {
            Log::error("通过code换取手机失败:");
            Log::error($curl_return_data);
            return $this->error('getPhoneNumber 获取失败');
        }
        $phoneNumber        = $curl_return_ary['phone_info']['phoneNumber'];
        Log::error("获取手机号成功success::".$phoneNumber);
        return $curl_return_ary;
    }

curl post函数,这个一定要用json格式啊

/**
 * curl post json 请求
 * */
function postCurl($url,$data,$json =false){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
        if($json && is_array($data)){
            $data = json_encode( $data );
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        if($json){ //发送JSON数据
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER,
                array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data)));}
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);
    if ($errorno) {
        return array('errorno' => false, 'errmsg' => $errorno);
    }
    curl_close($curl);
    return $res;
}

以上php代码可以解决以下这2类错误:

"errcode":43002,"errmsg":"require POST method hint: [pKNcxzI2e-z.AIza] rid:

这个是说必须要用post请求,而且请求参数要正确

"errcode":47001,"errmsg":"data format error hint:

这个是说请求的参数格式不正确,一般是传了数组,或者类似表单的键值对格式导致的。这里必须传json格式,所以报这个错。

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值