PHP WeChat public number development, access to user WeChat personal information

Source: Internet
Author: User
Tags openid
Many developers in the development of the public number, all need to get the user's avatar, if it is a personal account, you need to apply for "public platform test Account" to obtain the public number of all interface rights;

First to get sent over the Access_token, and then according to OpenID and Access_token, to get the server sent over the JSON format of the user message, and finally with Josn_decode to convert it can get the user's detailed message;

Before writing the code, you can test the interface operations such as:

First enter the public platform as shown in:


Click on the Developer tool, then click on the online interface to debug, as shown, enter the interface selection interface


Click to enter the page in the interface type select: User management, in the interface list select: Get user information interface/user/info;

Then review the development documentation to get Access_token to use AppID and Appsecret as


Appsecret Click Enable to get, then enter in the browser URL:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= $appid &secret= $appsecret "website , where $appid is the AppID string in, $appsecret is your developer password, after replacing two variables with a string (note the quotation marks) Click Enter to get the server sent over the Access_token and expires_in two data, Copy the obtained Access_token string to the Interface Debug page, OpenID is the test account attention platform after the only ID is encrypted, get the method see the previous article, or Baidu can also, Input two parameters into the corresponding input box in the interface page (after the input is completed, it will show a green pass to verify the word), and then click Detect problem can be;

The information obtained is as follows:


This means that you have successfully obtained, the following code to obtain Access_token and personal information;

Public Function Getaccss_token () {$table = ' access_token '; $useraccess _token = db::table (' Access_token ')->select (); $appid = "Your AppID"; $appsecret = "Your Appsecret"; $url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_ credential&appid= $appid &secret= $appsecret ";//judgment is not the first time to get Access_tokenif (!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)];D b::table ($table)->insert ($data); return $access _token;} else if ($useraccess _token[0][' Expires_time ']<time ()) {//determine if expiration $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);//conversion format $access_token = $jsoninfo ["Access_token"]; $data =[' access_token ' = $access _token, ' expires_time ' = > ($jsoninfo [' expires_in ']+time () -200)];D b::table ($table)->where (' Expires_time ', $useraccess _token[0][' Expires_time '])->update ($data);//update database; return $access _token;} else{$access _token = $useraccess _token[0][' access_token '];return $access _token;}

Through the curl function to intercept sent over the message, because the acquisition of the Access_token effective time only 7200s is two hours, so every time the user to the public input operation will be detected is not expired, if expired will be re-acquired, If a few if judgment is to judge, I will access_token and time are stored in the database, so that each time can be taken out of the detection, of course, you can also call from the cache or put in a file from the file;

Once you get to Access_token, use Access_token and OpenID to get user personal information such as functions:

Public Function UserInfo ($openid) {$access _token = $this->getaccss_token ();//Get user information address $urlid = ' https:// Api.weixin.qq.com/cgi-bin/user/info?access_token= '. $access _token. ' &openid= '. $openid. ' &lang=zh_cn '; $curl = Curl_init (); Start a Curl session 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); Skip certificate Check//curl_setopt ($curl, Curlopt_ssl_verifyhost, true);  Check that the SSL encryption algorithm exists from the certificate $tmpinfo = curl_exec ($curl);     Returns the JSON object for the API//close URL request curl_close ($curl);                $userinfo = Json_decode ($tmpInfo, True); return $userinfo;}

It's the same thing. By the curl function related operation to intercept the information, the conversion format can be, this last UserInfo is the user's personal information array, you only need to call on it;

The above is through the Access_token to obtain the user's personal information operation ~!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.