obtaining micro-channel openid php

The use of micro-channel interfaces, whether it is automatically logged or micro-channel pay first thing we need is to get openid, get openid in two ways, one way is to get attention at the time, this subscription number can get to, and the second is authorized to get through the pages, such access is needed is a certification service number.

Today I want to say is authorized to acquire the second page openid.

  . 1 ? < PHP
   2  / * *
   . 3  * authorization micro-channel interfaces
   . 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 is      / * *
 13 is       * Get micro-channel authorization link
 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       * Get the micro channel OpenID
 25       * / 
26 is      public  function getOpenid ( $ Turl )
 27      {
 28          IF (! Isset ( $ _GET [ 'code' ])) {
 29              // trigger the micro-channel return code CODE 
30              $ URL = the this $ -> get_authorize_url ( $ Turl , $ the this -> State);
 31 is              Header ( "the Location: $ URL " );
 32              Exit ();
 33 is          }the else {
 34 is              // Get code codes, to obtain OpenID 
35              $ code = $ _GET [ 'code' ];
 36              $ access_info = $ the this -> get_access_token ( $ code );
 37 [              return  $ access_info ;
 38 is          }
 39      }
 40      / * *
 41       * get authorization token authorizing website
 42 is       *
 43 is       * @param String $ code acquired by the code get_authorize_url
 44 is       * / 
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);
if53          ($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 & lt \ n-." ;
 87              print_r ( $ Response );
 88          }
 89          curl_close ( $ CI );
 90          return  Array ( $ HTTP_CODE , $ Response );
 91 is      }
 92  }
 93  
94  // getOpenid ($ Turl) this method is a method of obtaining openid.
95  // distal calling code as follows: 
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 ?>

 

Guess you like

Origin www.cnblogs.com/clubs/p/11649638.html