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

相关推荐
李纲明9 小时前
微信小程序选片一拖几个月或口头加片扯皮:先核截止日与节点提醒,再查加项确认与尾款
微信小程序·devdiy·选片截止·节点提醒·加项确认·尾款交付·摄影出片
FungLeo12 小时前
FengCMS · PHP+MySQL 升级实录 ·三重 MD5 迁移 password_hash:老站密码怎么无痛升级
mysql·php·哈希算法·密码安全·fengcms
ServBay13 小时前
PHP 8.6 最新发布:语法更新、实用函数与升级要点
后端·php
2401_8788510120 小时前
微信小程序实战:掌握“手机状态”感知与设备 API 全解析
智能手机·微信小程序·notepad++
FungLeo21 小时前
FengCMS ·PHP+MySQL 升级实录 · 安全加固的克制:一份“不做什么“清单,比修了几个漏洞更重要
mysql·安全·php·安全加固·fengcms
.痞老板.2 天前
java ee--网络编程套接字
网络·java-ee·php
马哥python说2 天前
我用DeepSeek Harness快速上线了一个情侣小程序
微信小程序·小程序·云开发
Jackzhang2 天前
[端与游戏开发手记] 小程序第一次调接口,卡在域名白名单的那半天
微信小程序
djxzyr2 天前
微信小程序传感器实战:罗盘、加速计、陀螺仪与加速度实时波形
微信小程序·小程序
JianZhen✓3 天前
IM 聊天列表为什么总“消息乱跳“:有序插入、时间轴派生、本地截断、已读回执
开发语言·前端·php