- <SPAN style="FONT-SIZE: 14px"> //得到订阅用户 (返回数组)
- public function GetUserList()
- {
- $strjson = $this -> GetUrlReturn("https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s");
- $openidarr= $strjson->data->openid;
- //print_r($openidarr); 调试
- return $openidarr;
- }
- //得到订阅用户详情(返回对象)
- public function GetUserDetail($openid)
- {
- $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid={$openid}";
- $strjson = $this -> GetUrlReturn($url);
- return $strjson;
- }
- /*
- *
- * 私有成员变量 存token值
- * 因为//access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。
- * 正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。
- */
- private $_token ;
- /*
- *
- * 私有方法
- *
- */
- //得到Token对象并写入到配置文件
- private function InitToken()
- {
- $url = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",APPID, SECRET);
- //echo APPID;
- $ch = curl_init(); //创建一个新url资源
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $a = curl_exec($ch);
- $strjson=json_decode($a);
- $token = $strjson->access_token;
- if (empty($token))
- {
- //修改 {"errcode":45009,"errmsg":"api freq out of limit"}
- echo "错误:取得token无效,可能是调用太频繁!"; //$strjson
- throw new Exception('错误:取得token无效');
- }
- $obj = fopen("saestor://weixindata/token.txt","w+"); //SAE禁用fopen本地文件,这里需要Storage
- fwrite($obj,$token);
- $this -> _token = $token;
- }
- //封装私有方法,调用得到Get的参数,$needToken默认为false, 不取值,这里有一个潜规则,%s为 self::$_token
- private function GetUrlReturn($url, $needToken = false)
- {
- //第一次为空,则从文件中读取
- if (empty($this -> _token))
- {
- $obj = fopen("saestor://weixindata/token.txt","r");
- $this -> _token = fgets($obj,1000);
- }
- //为空则重新取值
- if (empty($this -> _token) || $needToken)
- {
- $this ->InitToken();
- }
- $newurl = sprintf($url, $this -> _token);
- $ch = curl_init(); //创建一个新url资源
- curl_setopt($ch, CURLOPT_URL,$newurl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $a = curl_exec($ch);
- $strjson=json_decode($a);
- //var_dump($strjson); //开启可调试
- if (!empty($strjson-> errcode))
- {
- switch ($strjson-> errcode){
- case 40001:
- $this -> GetUrlReturn($url, true); //重新取值,可能是过期导致
- break;
- case 41001:
- throw new Exception("缺少access_token参数:".$strjson->errmsg);
- break;
- default:
- throw new Exception($strjson->errmsg); //其他错误,抛出
- break;
- }
- }
- return $strjson;
- }</SPAN>
防止Access_token过期的方法
最新推荐文章于 2024-04-23 21:07:25 发布