php獲取微信openid


使用微信接口,無論是自動登錄還是微信支付我們首先需要獲取的就是openid,獲取openid的方式有兩種,一種是在關注的時候進行獲取,這種訂閱號就可以獲取的到,第二種是通過網頁授權獲取,這種獲取需要的是認證服務號。

今天我要說的是第二種網頁授權獲取openid。

  1 <?php
  2 /**
  3  * 微信授權相關接口
  4  *
  5  * @link http://www.phpddt.com
  6  */
  7 class Wchat
  8 {
  9     private $app_id = 'wxaf04d17c0694fd82';
 10     private $app_secret = 'a3df3e828bf6307b2ed9daeb407ee624';
 11     private $state = 'aaaa';
 12     /**
 13      * 獲取微信授權鏈接
 14      *
 15      * @param string $redirect_uri 跳轉地址
 16      * @param mixed $state 參數
 17      */
 18     public function get_authorize_url($redirect_uri = '', $state = '')
 19     {
 20         $redirect_uri = urlencode($redirect_uri);
 21         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";
 22     }
 23     /**
 24      * 獲取微信openid
 25      */
 26     public function getOpenid($turl)
 27     {
 28         if (!isset($_GET['code'])) {
 29             //觸發微信返回code碼
 30             $url = $this->get_authorize_url($turl, $this->state);
 31             Header("Location: $url");
 32             exit();
 33         } else {
 34             //獲取code碼,以獲取openid
 35             $code = $_GET['code'];
 36             $access_info = $this->get_access_token($code);
 37             return $access_info;
 38         }
 39     }
 40     /**
 41      * 獲取授權token網頁授權
 42      *
 43      * @param string $code 通過get_authorize_url獲取到的code
 44      */
 45     public function get_access_token($code = '')
 46     {
 47         $appid = $this->app_id;
 48         $appsecret = $this->app_secret;
 49         $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";
 50         //echo $token_url;
 51         $token_data = $this->http($token_url);
 52         // var_dump( $token_data);
 53         if ($token_data[0] == 200) {
 54             $ar = json_decode($token_data[1], TRUE);
 55             return $ar;
 56         }
 57         return $token_data[1];
 58     }
 59     public function http($url, $method = '', $postfields = null, $headers = array(), $debug = false)
 60     {
 61         $ci = curl_init();
 62         /* Curl settings */
 63         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
 64         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
 65         curl_setopt($ci, CURLOPT_TIMEOUT, 30);
 66         curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
 67         switch ($method) {
 68             case 'POST':
 69                 curl_setopt($ci, CURLOPT_POST, true);
 70                 if (!empty($postfields)) {
 71                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
 72                     $this->postdata = $postfields;
 73                 }
 74                 break;
 75         }
 76         curl_setopt($ci, CURLOPT_URL, $url);
 77         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
 78         curl_setopt($ci, CURLINFO_HEADER_OUT, true);
 79         $response = curl_exec($ci);
 80         $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 81         if ($debug) {
 82             echo "=====post data======\r\n";
 83             var_dump($postfields);
 84             echo '=====info=====' . "\r\n";
 85             print_r(curl_getinfo($ci));
 86             echo '=====$response=====' . "\r\n";
 87             print_r($response);
 88         }
 89         curl_close($ci);
 90         return array($http_code, $response);
 91     }
 92 }
 93 
 94 //getOpenid($turl)這個方法就是獲取openid的方法。
 95 //前端調用代碼如下:
 96 $openid=isset($_COOKIE['openid'])?$_COOKIE['openid']:'';
 97 if(empty($openid))
 98 {
 99     $wchat=new wchat();
100     $t_url='http://'.$_SERVER['HTTP_HOST'].'/user.php?act=register';
101     $info=$wchat->getOpenid($t_url);
102     if($info){
103         $openid=$info['openid'];
104         setcookie('openid',$openid,time()+86400*30);
105     }
106 }
107 echo $openid;
108 ?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM