首頁 >php教程 >php手册 >微信授权验证类

微信授权验证类

WBOY
WBOY原創
2016-06-07 11:40:571331瀏覽

微信auth授权验证类,在别人的基础上修改的,有用的上的参考下
<?php <br />  <br> /**<br>  * 微信授权相关接口<br>  * <br>  * <br>  */<br>  <br> class wechatauth {<br>     <br>     //高级功能-》开发者模式-》获取<br>     private $app_id = 'wxa0c4*************';<br>     private $app_secret = 'e095b6dc**********************';<br>  <br>     /**<br>      * 获取微信授权链接<br>      * <br>      * @param string $redirect_uri 跳转地址<br>      * @param mixed $state 参数<br>      */<br>     public function get_authorize_url($redirect_uri = '', $state = '')<br>     {<br>         $redirect_uri = urlencode($redirect_uri);<br>         return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";<br>     }<br>     <br>     /**<br>      * 获取授权token<br>      * <br>      * @param string $code 通过get_authorize_url获取到的code<br>      */<br>     public function get_access_token($app_id = '', $app_secret = '', $code = '')<br>     {<br>         $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";<br>         $token_data = $this->http($token_url);<br>         <br>         if($token_data[0] == 200)<br>         {<br>             return json_decode($token_data[1], TRUE);<br>         }<br>         <br>         return FALSE;<br>     }<br>     <br>     /**<br>      * 获取授权后的微信用户信息<br>      * <br>      * @param string $access_token<br>      * @param string $open_id<br>      */<br>     public function get_user_info($access_token = '', $open_id = '')<br>     {<br>         if($access_token && $open_id)<br>         {<br>             $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";<br>             $info_data = $this->http($info_url);<br>             <br>             if($info_data[0] == 200)<br>             {<br>                 return json_decode($info_data[1], TRUE);<br>             }<br>         }<br>         <br>         return FALSE;<br>     }<br> <br>     /**<br>      * 验证授权<br>      * <br>      * @param string $access_token<br>      * @param string $open_id<br>      */<br>     public function check_access_token($access_token = '', $open_id = '')<br>     {<br>         if($access_token && $open_id)<br>         {<br>             $info_url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$open_id}&lang=zh_CN";<br>             $info_data = $this->http($info_url);<br>             <br>             if($info_data[0] == 200)<br>             {<br>                 return json_decode($info_data[1], TRUE);<br>             }<br>         }<br>         <br>         return FALSE;<br>     }<br> <br> <br>     //curl<br>     public function http($url, $method, $postfields = null, $headers = array(), $debug = false)<br>     {<br>         $ci = curl_init();<br>         /* Curl settings */<br>         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);<br>         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);<br>         curl_setopt($ci, CURLOPT_TIMEOUT, 30);<br>         curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);<br>  <br>         switch ($method) {<br>             case 'POST':<br>                 curl_setopt($ci, CURLOPT_POST, true);<br>                 if (!empty($postfields)) {<br>                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);<br>                     $this->postdata = $postfields;<br>                 }<br>                 break;<br>         }<br>         curl_setopt($ci, CURLOPT_URL, $url);<br>         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);<br>         curl_setopt($ci, CURLINFO_HEADER_OUT, true);<br>  <br>         $response = curl_exec($ci);<br>         $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);<br>  <br>         if ($debug) {<br>             echo "=====post data======\r\n";<br>             var_dump($postfields);<br>  <br>             echo '=====info=====' . "\r\n";<br>             print_r(curl_getinfo($ci));<br>  <br>             echo '=====$response=====' . "\r\n";<br>             print_r($response);<br>         }<br>         curl_close($ci);<br>         return array($http_code, $response);<br>     }<br>  <br> }放出输出的demo,有用的上的可以参考//微信认证链接<br>     public function wxurl(){<br>         import('MyClass.Wechatauth',APP_PATH);<br>         $Wechat = new wechatauth();<br>         $token = session('token'); //查看是否已经授权<br>         if (!empty($token)) {<br>             print_r($token);<br>             $state = $Wechat->check_access_token($token['access_token'],$token['openid']); //检验token是否可用(成功的信息:"errcode":0,"errmsg":"ok")<br>             print_r($state);<br>         }<br>         $url = $Wechat->get_authorize_url('http://twx.vjiankang.org/wsite/test/wxrurl','1'); //此处链接授权后,会跳转到下方处理<br>         echo '<a>授权</a>';<br> <br>     }<br>     <br>     //微信返回字符串<br>     public function wxrurl(){<br>         import('MyClass.Wechatauth',APP_PATH);<br>         $Wechat = new wechatauth();<br>         print_r($_GET); //授权成功后跳转到此页面获取的信息<br> <br>         $token = $Wechat->get_access_token('','',$_GET['code']); //确认授权后会,根据返回的code获取token<br>         print_r($token);<br>         session('token',$token); //保存授权信息<br> <br>         $user_info = $Wechat->get_user_info($token['access_token'],$token['openid']); //获取用户信息<br>         print_r($user_info);<br> <br>     }需要注意的是,我们用于返回的url,需要在微信公众平台-》开发者-》auth认证里修改为同一域名下

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn