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;
        }
    }
    ?>
  • 相关阅读:
    bnuoj 4207 台风(模拟题)
    bnuoj 4208 Bubble sort
    bnuoj 4209 Triangle(计算几何)
    bnuoj 33656 J. C.S.I.: P15(图形搜索题)
    bnuoj 33648 Neurotic Network(树形模拟题)
    bnuoj 33647 Angry Grammar Nazi(字符串)
    bnuoj 16493 Just Pour the Water(矩阵快速幂)
    Solidity合约记录——(三)如何在合约中对操作进行权限控制
    预赛第二场
    预赛第一场
  • 原文地址:https://www.cnblogs.com/dige1993/p/7078351.html
Copyright © 2011-2022 走看看