微信小程序获取手机号和openid

小程序通过wx.login组件会返回一个code,这个code用来获得用户的openid

小程序写法为:

wx.login({
  success (res) {
    if (res.code) {
      //发起网络请求
      wx.request({
        url: 'https://example.com/onLogin',// 后台给的请求地址
        data: {
          code: res.code
        }
      })
    } else {
      console.log('登录失败!' + res.errMsg)
    }
  }
})

 后端通过以下代码可以获得openid(参考文档:获取插件用户openpid | 微信开放文档)

curl_get($getUrl);//请求拼接好的url
        $wxResult = json_decode($result, true);
        return wxResult;
}
public function curl_get($url, &$httpCode = 0) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }

使用手机号快速验证组件,获得的code用来请求地址获得手机号。

这个是手机号快速验证组件



Page({
  getPhoneNumber (e) {
    console.log(e.detail.code)  // 动态令牌
    console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
    console.log(e.detail.errno)  // 错误码(失败时返回)
  }
})

具体返回信息为:

微信小程序获取手机号和openid_第1张图片

 后端拿到code后获取手机号的代码为(参考文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html):

public function getphone(){
        $js_code = input('post.phone_code');//小程序传来的code值
        $appid = '';//小程序的appid
        $appSecret = '';// 小程序的$appSecret
        $access_token = $this->getAccessToken($appid, $appSecret);
        if($access_token !== false){
            $data = array(
                'code' => $js_code
            );
            $result=$this->getUserPhoneNumber("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$access_token,$data);
            var_dump($result);
        }

    }
public function getAccessToken($appid,$secret){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        if(curl_errno($ch)){
            echo '
Curl error: ' . curl_error($ch)."
"; return curl_error($ch); } curl_close($ch); $result = json_decode($data,true); if(isset($result["access_token"])){ return $result["access_token"]; }else{ echo "
getAccessToken Failed: ".$result["errcode"].";".$result["errmsg"]."
"; return false; } } public function getUserPhoneNumber($remote_server, $data) { $json_data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); if(curl_errno($ch)){ echo '
Curl error: ' . curl_error($ch)."
"; return curl_error($ch); } curl_close($ch); //$result = json_decode($data,true); //var_dump($result); return $data; }

如还有问题,可以留言联系,处理了一晚上的bug就记录到这里啦

参考官方文档:

小程序端需要写的:

手机号快速验证组件 | 微信开放文档

php后台需要写的:

手机号验证 | 微信开放文档

获取接口调用凭据:

获取接口调用凭据 | 微信开放文档

 

你可能感兴趣的:(微信小程序,小程序)