php获取微信openid方法总结

时间:2022-09-23 20:24:21

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

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

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

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

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

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

以上就是php获取微信openid的详细内容,更多请关注服务器之家其它相关文章!