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

相关推荐
Irene19915 小时前
JavaScript脚本加载的两种方式:defer/async 的区别
前端·javascript·php
luanma1509805 小时前
PHP vs C#:30字秒懂两大语言核心差异
android·开发语言·python·php·laravel
志遥6 小时前
我把 Sentry 接进了 7 端小程序:从异常捕获、Breadcrumb 到 Source Map 定位
微信小程序·监控
fresh hacker6 小时前
【Linux系统】通用的“系统排障”
linux·运维·服务器·网络·php
luanma1509806 小时前
Laravel 8.X重磅特性全解析
前端·javascript·vue.js·php·lua
luanma1509806 小时前
Laravel 9.x重磅升级:十大新特性解析
php·laravel
zhangren024686 小时前
PHP vs C++:从Web脚本到系统编程的终极对比
开发语言·c++·php
luanma1509806 小时前
Laravel 7.X核心特性深度解析
android·开发语言·php·lua·laravel
云起SAAS6 小时前
在线客服系统源码 | 支持PC管理端+H5访客端+实时聊天
微信小程序·ai编程·看广告变现轻·在线客服系统源码
2501_933907217 小时前
如何通过上海本凡科技获得优质的小程序开发服务?
科技·微信小程序·小程序