PHP 微信公众号开发 - 获取用户信息
项目微信公众号开发,记录获取用户微信号信息,和用户openid
1,登录微信公众平台
2,获取公众号开发信息
登陆之后在 开发->基本配置
3,设置IP白名单
在这里添加服务器ip地址,并点击确认修改
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 }