开始
小程序端代码:
<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格式,所以报这个错。
结束