微信网页授权

来源:互联网 发布:suse 11 安装yum 编辑:程序博客网 时间:2024/06/02 08:15

/*

*网页授权

*/

public function actionAdd()

        {  
            if (isset($_GET['code'])) {

                $code=$_GET['code'];
                $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxbbfa6df80debe0ae&secret=b97b0ef0248af454e15a8e1ea29f0e6d&code=".$code."&grant_type=authorization_code";
                $url=(array)json_decode(file_get_contents($url));
                $access_token=$url['access_token'];
                $oppid=$url['openid'];

                $openid_url="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$oppid."&lang=zh_CN";

                $openid_url=file_get_contents($openid_url);
                file_put_contents("msg/ge.txt",$openid_url);
                var_dump($openid_url);

            }else{

                //回调地址
                $hd=urlencode("http://jmj.free.ngrok.cc/Month10/weixin/frontend/web/index.php?r=wei/add");

                $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbbfa6df80debe0ae&redirect_uri=".$hd."&response_type=code&scope=snsapi_userinfo&state=12345#wechat_redirect";

                header("Location:".$url);

            }

        }

// 获取token值
        public function actionAccess_token()
        {        
               //判断文件是否存在
                if (is_file("msg/token.txt")) {
                       $access_token=json_decode(file_get_contents("msg/token.txt"),true);
                       //判断时间是否过期
                         if (time()-$access_token['times']>7000) {
                             $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbbfa6df80debe0ae&secret=b97b0ef0248af454e15a8e1ea29f0e6d";
                            $token=file_get_contents($token_url);
                            $ken= json_decode($token,true);    
                            $ken['times']=time();                    
                         }
                            $access_token=$ken['access_token'];
                            return $access_token;                            
                     }else{
                             $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbbfa6df80debe0ae&secret=b97b0ef0248af454e15a8e1ea29f0e6d";
                            $token=file_get_contents($token_url);
                            $ken= json_decode($token,true);                        
                            file_put_contents("msg/token.txt",$ken);
                            $access_token=$ken['access_token'];
                            return $access_token;
                     }                
        }


// 封装curl
        public function https_request($url, $filedata)
        {
                $curl = curl_init ();
                curl_setopt ( $curl, CURLOPT_URL, $url );
                curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
                curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
                if (! empty ( $filedata)) {
                curl_setopt ( $curl, CURLOPT_POST, 1 );
                curl_setopt ( $curl, CURLOPT_POSTFIELDS,$filedata);
                }
                curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
                $output = curl_exec ( $curl );
                curl_close ( $curl );
                return $output;
        }





原创粉丝点击