当前位置:网站首页>Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
2022-07-29 02:59:00 【You are sexy!】
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use think\Hook;
use think\Db;
use think\Cache;
/**
*
*/
class Wechat extends Api
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
public $AppId = ' Fill in your own ';
public $Secret = ' Fill in your own ';
// Get the user's mobile number
public function user_phone(){
// Get the... From the front end code, If the front end doesn't know code What is it , Just tease him .
$post = $this->request->param();
if(!isset($post['code']) || empty($post['code'])){
$return['status'] = 222;
$return['msg'] = ' Illegal request ';
return json_encode($return);
}
// obtain accesstoken
$accessToken = $this->get_access_token();
// Request address
$url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
// From the front end code
$code = $post['code'];
// Make an array
$data=[
'code'=>$code,
];
// Here we need to convert the passed parameters into json, Otherwise, the applet interface will report data type error .
$result = json_decode($this->curl_post_https($url,json_encode($data)),true);
// Start to judge whether the acquisition is successful
if($result['errmsg'] == 0){
// To be successful
$phoen = $result['phone_info']['phoneNumber'];
$return['smg'] = ' Successfully obtained mobile phone number !';
$return['code'] = 200;
$return['phone'] = $phoen;
// Return the phone number to the front end , Or store it yourself . See the demand
//Db::name('user')->add();
return json_encode($return);
}else{
$return['smg'] = ' Failed to get mobile number !';
$return['code'] = 201;
return json_encode($return);
}
}
// Get the QR code of the applet token
public function get_access_token()
{
// First judge what is in the cache access_token Has it expired
if(Cache::get('access_token')){
// Take it out directly without expiration
$a = Cache::get('access_token');
return $a;
}else{
// Get it again when it expires
$appid = $this->AppId;
$secret = $this->Secret;
$url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=$appid&secret=$secret";
// Request interface , obtain accesstoken
$user_obj = $this->curlHttp($url);
// And then accesstoken Stored in cache , Official expiration time 7200 second , The cache can expire earlier , Control yourself
Cache::set('access_token',$user_obj['access_token'],7100);
return Cache::get('access_token');
}
}
/**
* send out get request
*/
public function curlHttp($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
// Release curl Handle
curl_close($ch);
return json_decode($output,true);
}
/**
* send out post request
*/
public function curl_post_https($url,$data,$header=[]){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}The data returned after the request is successful .

It's simple !
边栏推荐
- 【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
- PHP process communication series (I) named pipes
- seed 随机种子
- 常用hooks总结
- Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
- 融云实时社区解决方案
- Pgzero aircraft war
- [QNX hypervisor 2.2 user manual]9.11 RAM (under update)
- Double for loop
- CentOS install mysql8
猜你喜欢

navicat新建数据库

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

【机器人学习】机械臂抓手matlab运动学与admas动力学分析

Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers

万字详解 Google Play 上架应用标准包格式 AAB

数仓中概念术语解析

Zone --- line segment tree lazy marking board sub problem

解析机器人与人类情感共鸣的主观意识

Analysis of Project-based Learning Creativity in steam Education

Weekly recommended short videos: how to make product development more effective?
随机推荐
R语言ERROR: compilation failed for package ‘****‘
sqlilabs less-32~less-33
idea配置web容器与war打包
JVM基础入门篇一(内存结构)
Analyzing the subjective consciousness of emotional resonance between robots and human beings
FTP protocol details
Zone --- line segment tree lazy marking board sub problem
C语言程序设计 | 交换二进制数奇偶位(宏实现)
MongoDB索引 (3)
Confusion matrix learning notes
Unable to start after idea installation
Implementation principle of golang synergy
数仓中概念术语解析
瀚高数据库最佳实践配置工具HG_BP日志采集内容
HTB-Blocky
VIM common commands
【机器人学习】机械臂抓手matlab运动学与admas动力学分析
.net serialize enumeration as string
【FreeSwitch开发实践】media bug获取通话语音流
解析机器人与人类情感共鸣的主观意识