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

相关推荐
漏刻有时4 小时前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php
always_TT5 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
Immortal__y8 小时前
Upload-Labs 关卡防御方式
php
JonLee202010 小时前
适配 Yii 3.0,php-casbin/yii-permission 3.0 正式发布
安全·php·rbac·yii·权限·casbin
QH1392923188012 小时前
# R&S ZNB43 ZNA43 ZNA67矢量网络分析仪
开发语言·网络·php
名字还没想好☜14 小时前
Python concurrent.futures 实战:用 ThreadPoolExecutor 并发处理 + as_completed 收结果
数据库·python·php·并发
风样滴男人哟15 小时前
PHP特性之反射类ReflectionClass机制
android·开发语言·php
nvd1116 小时前
GCP L4 Passthrough 负载均衡器“假死超时”深度排查复盘
运维·php·负载均衡
2601_9657984717 小时前
ForumLab Review: Fast PHP Script for SEO Forum Sites
android·adb·php
姜太小白1 天前
【Linux】df -h 卡住问题的通用排查与解决方案总结
linux·运维·php