Home
WeChat Applet
Mini Program Development
How to obtain access_token under the mini program (code example)



How to obtain access_token under the mini program (code example)
The content of this article is about how to obtain access_token (code example) under the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
##
<button bindtap="token">获取token</button>
Copy after login
// pages/token/token.js Page({ data: { }, token:function(){ wx.request({ url: 'xxxx', data:{}, success:function(res){ console.log(res.data); //这里我是用的新浪云的sae,所以存在读写权限问题,打印出来的信息会存在告警 } }); } })
Copy after login
<?php exit();?> {"access_token":"","expire_time":0}
Copy after login
<?php //验证 function getToken(){ $timestamp = $_GET["timestamp"]; $nonce =$_GET["nonce"]; $token="hello hqtoken"; $signature=$_GET["signature"]; $array=array($timestamp,$nonce,$token); sort($array); $tmpstr=implode("",$array); $tmpstr=sha1($tmpstr); if($tmpstr == $signature){ echo $_GET["echostr"]; exit; } } //获取AccessToken function getAccessToken(){ $appid="你的appid"; $appsecret = "你的appsecret"; //文件存储 $data=json_decode(get_php_file("access_token.php")); if($data->expire_time<time()){ $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $res=json_decode(httpGet($url)); $access_token=$res->access_token; if($access_token){ $data->expire_time=time()+7000; $data->access_token=$access_token; set_php_file("access_token.php",json_encode($data)); } }else{ $access_token=$data->access_token; } return $access_token; } //公共的curl方法 function httpGet($url){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //如果用的是https,ssl安全验证 curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false); $res =curl_exec($curl); curl_close($curl); return $res; } function get_php_file($filename){ return trim(substr(file_get_contents($filename),15)); } function set_php_file($filename,$content){ $fp=fopen($filename,"w"); fwrite($fp,"<?php exit();?>".$content); fclose($fp); } echo getAccessToken(); ?>
Copy after login
The above is the detailed content of How to obtain access_token under the mini program (code example). For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1385
52

