使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是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 ?>