当前位置:首页站长学院后端开发小程序下如何获取access_token(代码示例)
企业营销,就选知企PROSAAS

小程序下如何获取access_token(代码示例)

本篇文章给大家带来的内容是关于小程序下如何获取access_token(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

<button bindtap="token">获取token</button>
// pages/token/token.js
Page({
  data: {

  },
  token:function(){
    wx.request({
      url: 'xxxx',
      data:{},
      success:function(res){
        console.log(res.data);
        //这里我是用的新浪云的sae,所以存在读写权限问题,打印出来的信息会存在告警
      }
    });
  }
})
<?php exit();?>
{"access_token":"","expire_time":0}
<?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();
?>

以上就是小程序下如何获取access_token(代码示例)的详细内容,更多请关注知企PROSAAS其它相关文章!

温馨提示:

文章标题:小程序下如何获取access_token(代码示例)

文章链接:https://ceshi.prosaas.cn/17021.html

更新时间:2018年10月29日

声明: 本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:973664285@qq.com我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读知企PROSAAS协议

给TA打赏
共{{data.count}}人
人已打赏
后端开发

微信小程序中如何渲染html内容(代码示例)

2018-10-25 16:29:07

后端开发

小程序中动态获取列表对象信息的代码示例

2018-10-29 16:49:04

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索