微信小程序最新版获取用户绑定的微信手机号
-
获取接口调用凭据:getAccessToken
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=' . self::$grant_type . '&appid=' . self::$appid . '&secret=' . self::$secret;
$html = file_get_contents($url);
var_dump($html);
-
获取手机号:getPhoneNumber
function getPhone($access_token, $code) { $post_data = ["code" => $code]; //post参数 $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token; $info = http_request($url, json_encode($post_data), 'json'); $tmpinfo = json_decode($info); $code = $tmpinfo->errcode ?? false; if ($code === 0) { $phone_info = $tmpinfo->phone_info; //手机号 $phoneNumber = $phone_info->phoneNumber; return $phoneNumber; } else { return false; } } http_request($url, $data = null) { $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)) { curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' )); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output; exit(); }