//获取微信access_token
public function access_token(){
$appid ='***********';
$secret = '******************';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$data = json_decode(file_get_contents($url,true),true);
$access_token = $data['access_token'];
// var_dump($access_token);
return $access_token;
}
//获取微信手机号
public function phone(){
$code = Request::instance()->post();//获取微信发送code
$access_token = $this->access_token();
function posturl($url,$data)
{
$data = json_encode($data);
$headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
}
$phone = posturl('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token,$code);
return json($phone);
}
返回数据
{"errcode":0,"errmsg":"ok","phone_info":{"phoneNumber":"17633594810","purePhoneNumber":"17633594810","countryCode":"86","watermark":{"timestamp":1650767034,"appid":"wx7bb502f80d1fa3a5"}}}