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

相关推荐
twcc_come12 小时前
安全策略配置实验报告
开发语言·php
weixin_BYSJ198713 小时前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
上海安当技术16 小时前
车间电脑指纹登录怎么做?从 FIDO2 公钥体系到统一身份认证的技术拆解
开发语言·电脑·php
海纳百川·纳海川1 天前
数字化升级,让废品回收更简单高效
大数据·微信小程序·小程序
VX_bysjlw9851 天前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
一只昀2 天前
基于微信小程序的咖啡店点餐小程序
微信小程序·小程序
大黄说说2 天前
接口频繁超时、报错?后端常见排查思路总结
开发语言·php
Sagittarius_A*2 天前
【RCELABS】Level 1~4 —— 基础篇
靶场·php·ctf·代码审计·rce
海天鹰2 天前
PHP换行
php
moonsims2 天前
星闪在跨域无人化系统作用
开发语言·php