PHP 微信公衆號開發 - 獲取用戶信息

項目微信公衆號開發,記錄獲取用戶微信號信息,和用戶openid

1,登陸微信公衆平臺

  點擊登陸微信公衆平臺php

2,獲取公衆號開發信息

  登錄以後在 開發->基本配置
html

 3,設置IP白名單

  在這裏添加服務器ip地址,並點擊確認修改json

 

 4,微信接口類

 

  1 <?php
  2 // 字符編碼
  3 header("Content-Type:text/html; charset=utf-8");
  4 
  5 // 微信接口類
  6 class WeChat{
  7     private static $appid;
  8     private static $appsecret;
  9 
 10     function __construct(){
 11         self::$appid = '';      // 開發者ID(AppID)
 12         self::$appsecret = '';  // 開發者密碼(AppSecret)
 13     }
 14 
 15     // 微信受權地址
 16     public static function getAuthorizeUrl($url){
 17         $url_link = urlencode($url);
 18         return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . self::$appid . "&redirect_uri={$url_link}&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
 19     }
 20 
 21     // 獲取TOKEN
 22     public static function getToken(){
 23         $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$appid . "&secret=" . self::$appsecret;
 24         $outputa = self::curlGet($urla);
 25         $result = json_decode($outputa, true);
 26         return $result['access_token'];
 27     }
 28 
 29     /**
 30      * getUserInfo 獲取用戶信息
 31      * @param  string $code         微信受權code
 32      * @param  string $weiwei_token Token
 33      * @return array
 34      */
 35     public static function getUserInfo($code, $weiwei_token){
 36         $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
 37         $access_token_json = self::curlGet($access_token_url);
 38         $access_token_array = json_decode($access_token_json, true);
 39         $openid = $access_token_array['openid'];
 40         $new_access_token = $weiwei_token;
 41 
 42         //全局access token得到用戶基本信息
 43         $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
 44         $userinfo_json = self::curlGet($userinfo_url);
 45         $userinfo_array = json_decode($userinfo_json, true);
 46         return $userinfo_array;
 47     }
 48 
 49     /**
 50      * addLog 日誌記錄
 51      * @param string $log_content 日誌內容
 52      */
 53     public static function addLog($log_content = ''){
 54         $data = "";
 55         $data .= "DATE: [ " . date('Y-m-d H:i:s') . " ]\r\n";
 56         $data .= "INFO: " . $log_content . "\r\n\r\n";
 57         file_put_contents('/wechat.log', $data, FILE_APPEND);
 58     }
 59 
 60     /**
 61      * 發送get請求
 62      * @param string $url 連接
 63      * @return bool|mixed
 64      */
 65     private static function curlGet($url){
 66         $curl = curl_init();
 67         curl_setopt($curl, CURLOPT_URL, $url);
 68         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 69         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 70         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 71         $output = curl_exec($curl);
 72         if(curl_errno($curl)){
 73             return 'ERROR ' . curl_error($curl);
 74         }
 75         curl_close($curl);
 76         return $output;
 77     }
 78 
 79     /**
 80      * 發送post請求
 81      * @param string $url 連接
 82      * @param string $data 數據
 83      * @return bool|mixed
 84      */
 85     private static function curlPost($url, $data = null){
 86         $curl = curl_init();
 87         curl_setopt($curl, CURLOPT_URL, $url);
 88         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 89         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 90         if(!empty($data)){
 91             curl_setopt($curl, CURLOPT_POST, 1);
 92             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 93         }
 94         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 95         $output = curl_exec($curl);
 96         curl_close($curl);
 97         return $output;
 98     }
 99 }
100 
101 /**
102  * get_page_url 獲取完整URL
103  * @return url
104  */
105 function get_page_url($type = 0){
106     $pageURL = 'http';
107     if($_SERVER["HTTPS"] == 'on'){
108         $pageURL .= 's';
109     }
110     $pageURL .= '://';
111     if($type == 0){
112         $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
113     }else{
114         $pageURL .= $_SERVER["SERVER_NAME"];
115     }
116     return $pageURL;
117 }
118 
119 // 獲取用戶openid
120 
121 // 微信接口類
122 $WeChat = new WeChat();
123 if(empty($_GET['code']) || !isset($_GET['code'])){
124     // 經過受權獲取code
125     $url = get_page_url();
126     $authorize_url = $WeChat->getAuthorizeUrl($url);
127     header("Location:{$authorize_url}"); // 重定向瀏覽器
128     exit();
129 }else{
130     // 獲取微信用戶信息
131     $code = $_GET['code'];
132     $weiwei_token = $WeChat->getToken(); // 獲取微信token
133     $user_info = $WeChat->getUserInfo($code, $weiwei_token);
134     print_r($user_info); // 用戶微信信息
135     print_r($user_info['openid']); // 用戶openid
136 }
相關文章
相關標籤/搜索