微信公众号开发——获取access_token(PHP版)

 更新时间:2020年11月20日 13:55  点击:483

废话不多说

直接贴代码

<?php
class TokenUtil {
    //获取access_token并保存到token.txt文件中
    public static function build_access_token(){
        $ch = curl_init(); //初始化一个CURL对象
		
		$appid="wx2e9f8435ebdb2322";
		$secret="323db114f02b2b5cdc249ca35a4bf1cc";
		//设置你所需要抓取的URL
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
        curl_setopt($ch, CURLOPT_URL,$url);
		//设置curl参数,要求结果是否输出到屏幕上,为true的时候是不返回到网页中,假设上面的0换成1的话,那么接下来的$data就需要echo一下。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = json_decode(curl_exec($ch));
        if($data->access_token){
			//打开token.txt文件,没有会新建
            $token_file = fopen("token.txt","w") or die("Unable to open file!");
			//重写tken.txt全部内容
            fwrite($token_file,$data->access_token);
            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;
    }
}
?>


[!--infotagslink--]

相关文章