当前位置:网站首页>微信小程序获取手机号
微信小程序获取手机号
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'];
}
边栏推荐
- redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)
- MySQL数据库实战(1)
- How to make self-introduction
- XDR平台架构与关键技术解析
- FR9811S6 SOT-23-6 23V, 2A Synchronous Step-Down DC/DC Converter
- Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)
- 卷起来!阿里高工携18位高级架构师耗时57天整合的1658页面试总结
- ABAB-740新语法
- 【网络原理的概念】
- Binary search tree (search binary tree) simulation implementation (there is a recursive version)
猜你喜欢
Web Server 设置缓存响应字段的一些推荐方案
基于PHP7.2+MySQL5.7的回收租凭系统
如何检索IDC研究报告?
机器学习(第一章)—— 特征工程
2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
Lease recovery system based on PHP7.2+MySQL5.7
袋鼠云思枢:数驹 DTengine,助力企业构建高效的流批一体数据湖计算平台
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
记住用户名案例(js)
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
随机推荐
RICON:NER SOTA 又来!
[错题]电路维修
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元
LeetCode——1161. 最大层内元素和
Why is the new earth blurred, in-depth analysis of white balls, viewing pictures, and downloading problems
complete knapsack problem
数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
3分钟实现内网穿透(基于ngrok实现)
MySQL数据库高级使用
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?...
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
程序员架构修炼之道:软件架构基本概念和思维
【JDBC以及内部类的讲解】
CDH6.3.2开启kerberos认证
ABAB-740新语法
【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制
【TypeScript】Why choose TypeScript?
【MySQL功法】第2话 · 数据库与数据表的基本操作
面试一面