Many developers need to obtain the user's avatar when developing a WeChat official account. If it is a personal WeChat account, you need to apply for a "public platform test account" to obtain a WeChat official account. All interface permissions;
First, you need to obtain the access_token sent by WeChat, and then use the openid and access_token to obtain the user message in json format sent by the WeChat server. Finally, use josn_decode to convert it to obtain the user's details. News;
Before writing the code, you can test the interface operation as shown below:
First enter the WeChat public platform, as shown below:
Click on Developer Tools, and then click on Online Interface Debugging, as shown in the figure below, to enter the interface selection interface
Click Then enter the page below and select in the interface type: User management, select in the interface list: Get user profile information interface/user/info;
Then check the WeChat development documentation to obtain access_token and use the appid and appsecret as shown below
public function getaccss_token(){ $table = 'Access_token'; $useraccess_token = Db::table('Access_token')->select(); $appid = "你的appid"; $appsecret = "你的appsecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; //判断是不是第一次获取access_token if(!count($useraccess_token)){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; $data=['access_token'=>$access_token,'expires_time'=>($jsoninfo['expires_in']+time()-200)]; Db::table($table)->insert($data); return $access_token; }else if($useraccess_token[0]['expires_time']<time()){//判断是否过期 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true);//转换格式 $access_token = $jsoninfo["access_token"]; $data=['access_token'=>$access_token,'expires_time'=>($jsoninfo['expires_in']+time()-200)]; Db::table($table)->where('expires_time',$useraccess_token[0]['expires_time'])->update($data);//更新数据库; return $access_token; }else{ $access_token = $useraccess_token[0]['access_token']; return $access_token; } }
public function userinfo($openid){ $access_token = $this->getaccss_token(); //获取用户信息地址 $urlid = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $urlid); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在 $tmpInfo = curl_exec($curl); //返回api的json对象 //关闭URL请求 curl_close($curl); $userinfo = json_decode($tmpInfo,true); return $userinfo; }
The above is the detailed content of php WeChat public account development to obtain user WeChat personal information. For more information, please follow other related articles on the PHP Chinese website!