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. PHP 程序获取 手机号码

    2024-01-10 08:42:04       60 阅读
  2. 程序手机号码授权登录

    2024-01-10 08:42:04       29 阅读
  3. springboot 解析程序获取手机

    2024-01-10 08:42:04       38 阅读
  4. 程序:登录,获取用户信息及手机号详解

    2024-01-10 08:42:04       22 阅读
  5. PHP 程序发货管理

    2024-01-10 08:42:04       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-10 08:42:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-10 08:42:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-10 08:42:04       87 阅读
  4. Python语言-面向对象

    2024-01-10 08:42:04       96 阅读

热门阅读

  1. RNN 和 Transformer 复杂度比较

    2024-01-10 08:42:04       51 阅读
  2. centos7.9 oracle 19c 安装

    2024-01-10 08:42:04       45 阅读
  3. 服务器宕机要怎么解决

    2024-01-10 08:42:04       53 阅读
  4. socket通信实现TCP协议的同步通信

    2024-01-10 08:42:04       56 阅读
  5. 关于数据库切换的麻烦

    2024-01-10 08:42:04       61 阅读
  6. Mysql in查询优化

    2024-01-10 08:42:04       57 阅读
  7. 正则表达式

    2024-01-10 08:42:04       44 阅读
  8. Tomcat服务实例部署

    2024-01-10 08:42:04       44 阅读
  9. ES6 新增 Set、Map 两种数据结构的理解

    2024-01-10 08:42:04       51 阅读
  10. LeetCode_4_困难_寻找两个正序数组的中位数

    2024-01-10 08:42:04       52 阅读
  11. 一、数据结构

    2024-01-10 08:42:04       58 阅读