当前位置:首页 > PHP教程 > php高级应用 > 列表

php获取微信openid方法总结

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-30 11:17:07 浏览: 评论:0 

在本篇文章里小编给大家整理了关于php获取微信openid方法总结,有需要的朋友们参考学习下。

使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的是认证服务号。

今天我要说的是第二种网页授权获取openid。下面是我写的一个关于获取openid的类

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * 微信授权相关接口 
  6.  
  7.  *  
  8.  
  9.  * @link http://www.phpfensi.com 
  10.  
  11.  */ 
  12.  
  13. class Wchat 
  14.  
  15. { 
  16.  
  17.    private $app_id = 'wx444444444444'; 
  18.  
  19.    private $app_secret = '77777777'; 
  20.  
  21.    private $state='aaaa'; 
  22.  
  23.   /** 
  24.  
  25.    * 获取微信授权链接 
  26.  
  27.    *  
  28.  
  29.    * @param string $redirect_uri 跳转地址 
  30.  
  31.    * @param mixed $state 参数 
  32.  
  33.    */ 
  34.  
  35.   public function get_authorize_url($redirect_uri = '', $state = '') 
  36.  
  37.   { 
  38.  
  39.     $redirect_uri = urlencode($redirect_uri); 
  40.  
  41.     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"; 
  42.  
  43.   } 
  44.  
  45.    /** 
  46.  
  47.    * 获取微信openid 
  48.  
  49.    */ 
  50.  
  51.   public function getOpenid($turl) 
  52.  
  53.   { 
  54.  
  55.     if (!isset($_GET['code'])){ 
  56.  
  57.       //触发微信返回code码 
  58.  
  59.          
  60.  
  61.        $url=$this->get_authorize_url($turl, $this->state); 
  62.  
  63.          
  64.  
  65.       Header("Location: $url"); 
  66.  
  67.       exit(); 
  68.  
  69.     } else { 
  70.  
  71.       //获取code码,以获取openid 
  72.  
  73.       $code = $_GET['code']; 
  74.  
  75.       $access_info = $this->get_access_token($code); 
  76.  
  77.       return $access_info; 
  78.  
  79.     } 
  80.  
  81.        
  82.  
  83.   } 
  84.  
  85.   /** 
  86.  
  87.    * 获取授权token网页授权 
  88.  
  89.    *  
  90.  
  91.    * @param string $code 通过get_authorize_url获取到的code 
  92.  
  93.    */ 
  94.  
  95.   public function get_access_token($code = '') 
  96.  
  97.   { 
  98.  
  99.    $appid=$this->app_id; 
  100.  
  101.    $appsecret=$this->app_secret; 
  102.  
  103.       
  104.  
  105.     $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; 
  106.  
  107.     //echo $token_url; 
  108.  
  109.     $token_data = $this->http($token_url); 
  110.  
  111.     // var_dump( $token_data); 
  112.  
  113.     if($token_data[0] == 200) 
  114.  
  115.     { 
  116.  
  117.       $ar=json_decode($token_data[1], TRUE); 
  118.  
  119.       return $ar; 
  120.  
  121.     } 
  122.  
  123.        
  124.  
  125.     return $token_data[1]; 
  126.  
  127.   } 
  128.  
  129.      
  130.  
  131.      
  132.  
  133.   public function http($url, $method='', $postfields = null, $headers = array(), $debug = false) 
  134.  
  135.   { 
  136.  
  137.     $ci = curl_init(); 
  138.  
  139.     /* Curl settings */ 
  140.  
  141.     curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
  142.  
  143.     curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); 
  144.  
  145.     curl_setopt($ci, CURLOPT_TIMEOUT, 30); 
  146.  
  147.     curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); 
  148.  
  149.    
  150.  
  151.     switch ($method) { 
  152.  
  153.       case 'POST': 
  154.  
  155.         curl_setopt($ci, CURLOPT_POST, true); 
  156.  
  157.         if (!emptyempty($postfields)) { 
  158.  
  159.           curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); 
  160.  
  161.           $this->postdata = $postfields; 
  162.  
  163.         } 
  164.  
  165.         break; 
  166.  
  167.     } 
  168.  
  169.     curl_setopt($ci, CURLOPT_URL, $url); 
  170.  
  171.     curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); 
  172.  
  173.     curl_setopt($ci, CURLINFO_HEADER_OUT, true); 
  174.  
  175.    
  176.  
  177.     $response = curl_exec($ci); 
  178.  
  179.     $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); 
  180.  
  181.    
  182.  
  183.     if ($debug) { 
  184.  
  185.       echo "=====post data======\r\n"; 
  186.  
  187.       var_dump($postfields); 
  188.  
  189.    
  190.  
  191.       echo '=====info=====' . "\r\n"; 
  192.  
  193.       print_r(curl_getinfo($ci)); 
  194.  
  195.    
  196.  
  197.       echo '=====$response=====' . "\r\n"; 
  198.  
  199.       print_r($response); 
  200.  
  201.     } 
  202.  
  203.     curl_close($ci); 
  204.  
  205.     return array($http_code, $response); 
  206.  
  207.   } 
  208.  
  209.    
  210.  
  211. } 
  212.  
  213. ?> 

getOpenid($turl)这个方法就是获取openid的方法,前端调用代码如下:

  1. $openid=isset($_COOKIE['openid'])?$_COOKIE['openid']:''; 
  2.  
  3.    if(emptyempty($openid)) 
  4.  
  5.    { 
  6.  
  7.      $wchat=new wchat(); 
  8.  
  9.      $t_url='http://'.$_SERVER['HTTP_HOST'].'/user.php?act=register'; 
  10.  
  11.      $info=$wchat->getOpenid($t_url); 
  12.  
  13.      if($info){ 
  14.  
  15.         $openid=$info['openid']; 
  16.  
  17.       setcookie('openid',$openid,time()+86400*30);   
  18.  
  19.      } 
  20.  
  21.    } 

以上就是我总结的获取openid的方法啦。

Tags: php获取微信openid

分享到: