PHP 微信小程序获取 手机号码

PHP代码

php 复制代码
$param = $_POST;
$app_id = "";
$app_secret= "";

 $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $app_id . '&secret=' . $app_secret;

 $tmptoken = json_decode(curl($url_get), true);

 $token = $tmptoken['access_token'];

 $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $token;
 $data = [];
 $data['code'] = $param['code'];
 $info = curlPost($url,json_encode($data));
 $tmpinfo = json_decode($info, true);

 $code = $tmpinfo['errcode'];

 $phoneNumber = "";
 $phoneNumber = $tmpinfo['phone_info']['phoneNumber'] ?? "";

 if ($code == '0' && $phoneNumber) {
     echo $phoneNumber;
 } else {
     echo "获取失败";
 }

function curlPost($url, $data = [], $aHeader = "")
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	if ($aHeader) {
		curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
	}
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$result = curl_exec($ch);
	curl_close($ch);

	return $result;
}

小程序获取手机号文档

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html

相关推荐
TE-茶叶蛋1 天前
ThinkPHP入门
php
TE-茶叶蛋1 天前
结合登录页-PHP基础知识点解析
android·开发语言·php
流觞 无依1 天前
DedeCMS plus/comment.php 评论 XSS/注入(XSS、SQL注入)修复教程
sql·php·xss
cyber_两只龙宝1 天前
【Nginx】Nginx实现FastCGI详解
linux·运维·nginx·云原生·php·memcached·fastcgi
dog2501 天前
卡瓦列里积分赏析
开发语言·php
5G丶1 天前
ThinkPHP 集群部署完整指南
数据库·php
流觞 无依1 天前
DedeCMS plus/recommend.php SQL注入漏洞修复教程
sql·php
流觞 无依1 天前
DedeCMS plus/list.php 参数注入(SQL注入)修复教程
sql·安全·php
软件开发技术1 天前
最新网络游戏账户交易平台系统源码 全开源版本 全新UI 自适应移动端
php