美文网首页
微信php接口-备注

微信php接口-备注

作者: leptune | 来源:发表于2024-01-18 14:47 被阅读0次
    <?php
    namespace app\api\controller;
    
    class Wx extends Base{
        const APPID = 'APPID';
        const APP_SECRET = 'APP_SECRET';
    
        public function get_openid(){
            if (!isset($this->post['js_code'])) {
                return json(['code' => 1, 'msg' => 'js_code必填']);
            }
            $res = $this->httpGet('https://api.weixin.qq.com/sns/jscode2session?appid='.self::APPID.'&secret='.self::APP_SECRET.'&js_code='.$this->post['js_code'].'&grant_type=authorization_code');
            return json(['code' => '0000', 'msg' => '成功', 'data' => json_decode($res,true)]);
        }
    
        private function get_access_token() {
            $access_token = cache('wx_access_token_'.self::APPID);
            if ($access_token) {
                return $access_token;
            }
            $res = $this->httpGet('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::APPID.'&secret='.self::APP_SECRET);
            $data = json_decode($res, true);
            if (isset($data['access_token'])) {
                cache('wx_access_token_'.self::APPID, $data['access_token'], $data['expires_in']);
                return $data['access_token'];
            }
            exit(__LINE__.':'.$res);
        }
    
        public function get_userinfo() {
            if (!isset($this->post['openid'])) {
                return json(['code' => 1, 'msg' => 'openid必填']);
            }
            $maxTry = 10;
            // 微信的判断是否关注的接口有时候会抽风,所以要多试几次
            $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->get_access_token().'&openid='.$this->post['openid'].'&lang=zh_CN';
            for ($i=0; $i < $maxTry; $i++) { 
                $tmpres = $this->httpGet($url);
                $res = json_decode($tmpres, true);
                if (isset($res['subscribe'])) {
                    return $res;
                }
                usleep(100*1000);
            }
            file_put_contents('lee.txt', '[error]:'.$tmpres, FILE_APPEND);
            // 最终微信接口还是失败了(暂时查不出什么原因。。),那就只能默认返回的是已关注的结果了,代价是下次进入时,可能会提示需要关注
            $data = [
                'subscribe' => 1,
                'openid' => $this->post['openid'],
                'nickname' => '',
                'sex' => 0,
                'province' => '',
                'city' => '',
                'country' => '',
                'headimgurl' => '',
            ];
            return json(['code' => '0000', 'msg' => '成功', 'data' => $data]);
        }
    
        private function httpGet($url) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
            // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
            // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            @curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
            curl_setopt($curl, CURLOPT_URL, $url);
    
            $res = curl_exec($curl);
            curl_close($curl);
    
            return $res;
        }
    }
    

    相关文章

      网友评论

          本文标题:微信php接口-备注

          本文链接:https://www.haomeiwen.com/subject/eztdodtx.html