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

相关推荐
Cyber4K1 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee1 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
Johnstons7 小时前
Wireshark 和 tcpdump 到底怎么选?网络故障排查实战中的边界、判断标准与落地清单
wireshark·php·es·tcpdump·抓包分析·抓包与协议分析工具选型
暮雨疏桐8 小时前
阿里云从 OSS 将 CSV 推送至内网机器方案
阿里云·云计算·php
laomocoder10 小时前
Project-Nexus-WAN-跨公网Agent对话
开发语言·php
d111111111d12 小时前
MQTT+STM32+ESP8266网络程序分层+韦老师
笔记·stm32·单片机·嵌入式硬件·学习·php
Lsx_13 小时前
H5 嵌入微信 / 支付宝 / 抖音小程序 WebView:调用原生能力完整方案
前端·微信小程序·webview
听海边涛声1 天前
phpStudy的下载、安装
php·phpstudy
知识领航员1 天前
2026年推荐6个AI音乐工具
java·人工智能·python·eclipse·django·php·pygame
计算机学姐1 天前
基于微信小程序的图书馆座位预约系统【uniapp+springboot+vue】
vue.js·spring boot·微信小程序·小程序·java-ee·uni-app·intellij-idea