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

相关推荐
2601_9498049216 分钟前
开源多商户商城源码最新版_适配微信小程序+H5+APP+PC多端
微信小程序·小程序
会开花的二叉树18 分钟前
Reactor网络库的连接管理核心:Connection类
开发语言·网络·php
码云数智-大飞24 分钟前
2026主流自助建站平台对比评测
微信小程序
木子啊2 小时前
PHP中间件:ThinkCMF 6.x核心利器解析
开发语言·中间件·php
Big Cole2 小时前
PHP面试题(核心基础篇:垃圾回收+自动加载)
android·开发语言·php
Diros1g3 小时前
ubuntu多网卡网络配置
网络·ubuntu·php
catchadmin3 小时前
PHP 现在可以零成本构建原生 iOS 和 Android 应用 NativePHP for Mobile v3 发布
android·ios·php
2501_933907214 小时前
上海本凡科技的微信小程序公司主要提供哪些服务?
科技·微信小程序·小程序
子木鑫4 小时前
[SUCTF 2019] CheckIn1 — 利用 .user.ini 与图片马构造 PHP 后门并绕过上传检测
android·开发语言·安全·php
JSON_L4 小时前
使用 SQLite 创建数据库和表
数据库·sqlite·php