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

相关推荐
发量惊人的中年网工1 小时前
分公司访问总部ERP慢怎么办?SD-WAN组网的排查与验收要点
服务器·开发语言·php
益莱储中国3 小时前
深度解析:RF 实验室从 5G迈向 6G 的过程中保持技术领先与设备升级的路径
开发语言·php
吹什么轩4 小时前
linux系统复习:僵尸进程与孤儿进程
linux·运维·php
专注仿真5 小时前
ThinkPHP6 API接口开发实例:从零搭建RESTful风格接口
后端·php·restful
创了么OPC5 小时前
小程序能收款了,对公户还没开?独立开发者的交付闭环踩坑②
前端·微信小程序
郭萌6967 小时前
多商户平台的分销佣金:为什么同一笔订单,换个时间跑会算出另一个人
mysql·php
一木 之林7 小时前
微信小程序开发全攻略·文章版|05 路由·交互·网络与音乐播放器实战
网络·微信小程序·小程序·交互
4SAPI7 小时前
企业Agent落地选型:大模型API接入方案迁移实践与稳定性评估
人工智能·python·php
西索斯coding8 小时前
Grok 4.7 调用一直 401 怎么办?不是 Key 过期,是 Authorization 头格式问题——排查全流程 + 修复示例
服务器·开发语言·ai·php
niucloud-admin9 小时前
NIUCLOUD LITE AI开发文档—模块开发:微信小程序
微信小程序·小程序·notepad++