当前位置:网站首页>微信小程序获取手机号
微信小程序获取手机号
2022-08-03 11:09:00 【龍九^】
微信小程序获取手机号
流程
小程序端获取到code码之后发送请求到后端并携带code码,使用auth.getAccessToken发起GET请求获取到ACCESS_TOKEN,继续使用phonenumber.getPhoneNumber获取到用户手机号。
小程序端获取code码
wxml添加按钮
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
js添加函数
Page({
getPhoneNumber (e) {
wx.request({
url: 'url',
method:'POST',
data:{
code:e.detail.code,
},
success(res){
console.log(res);
}
})
}
})
后台获取到code进行处理
函数解释:
file_get_contents:发起请求来处理
json_decode:json格式转换为数组
stream_context_create:改变发送请求的方式为post
json_encode:数组转换为json格式
以上为在本例起到的作用
public function login(Request $request){
$APPID = env('APPID');
$APPSECRET = env('APPSECRET');
$ACCESS_TOKEN = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET"),true)['access_token'];
$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$ACCESS_TOKEN";
$content = stream_context_create(['http'=>['method' => 'POST' , 'header' => 'Content-type: application/json','content'=>json_encode(['code' => $request->code])]]);
$response = json_decode(file_get_contents($url , false , $content),true);
return $response['phone_info']['phoneNumber'];
}
边栏推荐
- MySQL数据库实战(1)
- 直播弱网优化
- Advanced use of MySQL database
- LeetCode——1161. 最大层内元素和
- Binary search tree (search binary tree) simulation implementation (there is a recursive version)
- MATLAB程序设计与应用 2.7 结构数据与单元数据
- What is the relationship between The Matrix and 6G?
- The effects of the background and the Activiti
- Activiti产生的背景和作用
- Cookie和Session使用
猜你喜欢

Generate interface documentation online

2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持

多态详细讲解(简单实现买票系统模拟,覆盖/重定义,多态原理,虚表)

Fastjson反序列化
![[Bubble sort and odd-even sorting]](/img/89/d63afe1900a05b2a5615fcc3c09ccb.png)
[Bubble sort and odd-even sorting]

干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换

在 Chrome 开发者工具里通过 network 选项模拟网站的离线访问模式

SmobilerService 推送实现

LyScript 实现对内存堆栈扫描
随机推荐
白帽黑客与留守儿童破壁对“画”!ISC、中国光华科技基金会、光明网携手启动数字安全元宇宙公益展
【Star项目】小帽飞机大战(九)
3分钟实现内网穿透(基于ngrok实现)
通过组策略安装软件和删除用户配置文件
MySQL - 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded
Android 技术面试准备(含面试题及答案)
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
Babbitt | Metaverse daily must-read: Players leave, platforms are shut down, and the digital collection market is gradually cooling down. Where is the future of the industry?...
build --repot
嵌入式软件组件经典架构与存储器分类
MATLAB programming and application 2.7 Structural data and unit data
直播弱网优化
SAP 电商云 Spartacus UI 的 External Routes 设计明细
Machines need tokens more than people
How to use outside the PHP command in the container
Generate interface documentation online
What is the relationship between The Matrix and 6G?
实至名归!九章云极DataCanvas公司荣获智能制造领域多项殊荣
二叉搜索树(搜索二叉树)模拟实现(有递归版本)
微信多开批处理(自动获取安装路径)