zoukankan      html  css  js  c++  java
  • 微信公众号开发——获取access_token(PHP版)

    access_token是调用微信接口的唯一凭据,每两小时刷新一次,我们需要每两小时就获取一次access_token。

    <?php
    class TokenUtil {
        //获取access_token并保存到token.txt文件中
        public static function build_access_token(){
            $ch = curl_init(); //初始化一个CURL对象
            curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2e9f8435ebdb2856&secret=288db114f02b2b5cdc249ca75a4bf1cc");//设置你所需要抓取的URL
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置curl参数,要求结果是否输出到屏幕上,为true的时候是不返回到网页中,假设上面的0换成1的话,那么接下来的$data就需要echo一下。
            $data = json_decode(curl_exec($ch));
            if($data->access_token){
                $token_file = fopen("token.txt","w") or die("Unable to open file!");//打开token.txt文件,没有会新建
                fwrite($token_file,$data->access_token);//重写tken.txt全部内容
                fclose($token_file);//关闭文件流
            }else{
                echo $data->errmsg;
            }
            curl_close($ch);
        }
        
        //设置定时器,每两小时执行一次build_access_token()函数获取一次access_token
        public static function set_interval(){
            ignore_user_abort();//关闭浏览器仍然执行
            set_time_limit(0);//让程序一直执行下去
            $interval = 7200;//每隔一定时间运行
            do{
                build_access_token();
                sleep($interval);//等待时间,进行下一次操作。
            }while(true);
        }
        
        //读取token
        public static function read_token(){
            $token_file = fopen("token.txt", "r") or die("Unable to open file!");
            $rs = fgets($token_file);
            fclose($token_file);
            return $rs;
        }
    }
    ?>
  • 相关阅读:
    jquery 绑定事件前先解除事件绑定
    jquer ajax的方法返回值
    jQuery动态生成不规则表格前后端
    常见的正则表达式
    锚点连接
    javascript动态添加删除表格
    java面试题之第二回
    [转]java抽象类和接口
    Java IO 和 NIO
    Java IO之序列化
  • 原文地址:https://www.cnblogs.com/dige1993/p/7078351.html
Copyright © 2011-2022 走看看