关注微信时获取用户信息

关注微信公共号时获取用户信息
只需获取到 openID 便可经过 下面接口 获取用户信息 固然你也能够经过网页受权的方式拿到 openIDphp

1 接口调用请求说明
2 http请求方式: GET https://api.weixin.qq.com/cgi-bin/user/info?access\_token=ACCESS\_TOKEN&openid=OPENID&lang=zh\_CN

  

 1  private function _responseMsg() 
 2     {
 3         $post_str = file_get_contents('php://input');
 4         if (!empty($post_str)) {
 5             // 解析微信传过来的 XML 内容
 6             $post_obj      = (array)simplexml_load_string($post_str, 'SimpleXMLElement', LIBXML_NOCDATA);
 7             $from_username = $post_obj['FromUserName'];
 8             $to_username   = $post_obj['ToUserName'];
 9             $MsgType       = $post_obj['MsgType'];    //消息类型,event
10             $Event         = $post_obj['Event'];      //事件类型,subscribe(订阅)、unsubscribe(取消订阅)
11             $EventKey      = $post_obj['EventKey'];   //扫码关注时 带的信息

这里的 $post_obj['FromUserName'] 就是 当前用户的 openIDapi

 1 /**
 2      * [http_curl description]
 3      * @param  [type]  $url        [description]
 4      * @param  string  $method     [description]
 5      * @param  [type]  $postfields [description]
 6      * @param  array   $headers    [description]
 7      * @param  boolean $debug      [description]
 8      * @return [type]              [description]
 9      */
10     public function http_curl($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false)
11     {
12         $ci = curl_init();
13         /* Curl settings */
14         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
15         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
16         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
17         curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
18         curl_setopt($ci, CURLOPT_TIMEOUT, 30);
19         curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
20 
21         switch ($method) {
22             case 'POST':
23                 curl_setopt($ci, CURLOPT_POST, true);
24                 if (!empty($postfields)) {
25                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
26                     $this->postdata = $postfields;
27                 }
28                 break;
29         }
30         curl_setopt($ci, CURLOPT_URL, $url);
31         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
32         curl_setopt($ci, CURLINFO_HEADER_OUT, true);
33 
34         $response = curl_exec($ci);
35         $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
36 
37         if ($debug) {
38             echo "=====post data======\r\n";
39             var_dump($postfields);
40 
41             echo '=====info=====' . "\r\n";
42             print_r(curl_getinfo($ci));
43 
44             echo '=====$response=====' . "\r\n";
45             print_r($response);
46         }
47         curl_close($ci);
48         return array($http_code, $response);
49     }
相关文章
相关标签/搜索