PHP Micro-trust public platform development to obtain the user basic information _php instance

Source: Internet
Author: User
Tags cdata openid

The method in this article will include subscription number and service number and custom menu scenarios, regardless of whether there is advanced interface permissions, there are ways to obtain user profile, without the need to simulate login.

The combination of micro-mail and Third-party Web sites is a common solution for many customers. Here is a brief introduction to obtain the basic information of micro-users, and in the Third-party Web site directly login.

When the micro-trust is concerned about a public number, there will be a unique OpenID, this time we need to use it to request the micro-trust server to obtain the user's basic information, including Avatar, nickname and so on.

Please refer to the micro-credit development documentation for more detailed instructions.

Get the following steps:

1 First step: User consent authorization, get code
2 Second step: Through code in exchange for Web page authorization Access_token
3 Step Three: Refresh Access_token (if required)
4 Fourth Step: Pull user information (need scope for snsapi_userinfo)
5 attached: Verify the validity of the authorization voucher (Access_token)

First, in the micro-trust public platform--Developer Center: Web page Authorization to obtain user basic information: Click "Modify", configure the page callback domain name

Second, the user authorization to obtain code

Required Resources:

$appid = ' ***************** ';
$appsecret = ' ************************* ';

Note: Redirect_url is a callback link address that is redirected after authorization, please use UrlEncode to process the link.

At the entrance of the website plus the configured $url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $appid. ' &redirect_uri=http%3a%2f%2fjixian.c.zmit.cn%2f&response_type=code&scope=snsapi_userinfo&state= 123&connect_redirect=1#wechat_redirect ';

and request access to the appeal URL.

When the user authorized, callback set the domain name, and will be in the URL parameter stitching we need the code, we directly use $_get[' code ' to get it!

Third, through the code to obtain Web page authorization Access_token and OpenID

$token _url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $appsecret. ' &code= '. $code. ' &grant_type=authorization_code ';
$token = Json_decode (file_get_contents ($token _url));
$opendid = $token->openid;
$access _token = $token->access_token;

Iv. Obtaining User Information

$info _url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token. ' &openid= '. $opendid. ' ⟨=ZH_CN ';
$info = Json_decode (file_get_contents ($info _url));
$data [' name '] = $info->nickname;
$data [' image '] = $info->headimgurl;
Print_r ($info);

Five, when the user's OpenID and other information, we can put it into the database, as long as there is OpenID, the equivalent of micro-trust users have logged on to the site!

Obtain user profile through global access token using the Access_token obtained by AppID and Appsecret

1. Users ' OpenID can be obtained when they are concerned and reply to the message.

<xml>
 <tousername><![ Cdata[gh_b629c48b653e]]></tousername>
 <fromusername><![ Cdata[ollb4jv7la3tydjvijp5v9qtu_ka]]></fromusername>
 <createtime>1372307736</createtime >
 <msgtype><![ Cdata[event]]></msgtype>
 <event><![ Cdata[subscribe]]></event>
 <eventkey><![ Cdata[]]></eventkey>
</xml>

One of the Fromusername is OpenID.

2. Then use the Access_token interface to request access to the global access Token

Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
return Result:

{"
 Access_token": "Nu7kr6v9l9tqaqm5ne3otpcttzx797wxw4snd2wl2hhbqlcixldvow2l-se0i-wmollniaylawzhbyhxnjblc_ Kaa092cxkmpj5fpuqno0il7bb0exz5s5qc9umypy-rz2y441w9qgfnmntizwsjsq ",
 " expires_in ": 7200
}

3. Use global Access_token to get more information about OpenID

Https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID
Returned as follows:

{
"Subscribe": 1,
"OpenID": "OLVPPJQS2BHVZWPJ5A-VTYAX4GLC",
"Nickname": "Hedgehog Baby",
"Sex": 1,
"Language": "ZH_CN",
"City": "Shenzhen",
"Province": "Guangdong",
"Country": "China",
"Headimgurl": "http://wx.qlogo.cn/mmopen/ Jcdicrzblrehnnxzrudod9pmibrkis5k2f1tuq7lfjc63pyhaxgxndgmzjgdeuvzyzbfoqtuxaxsdozg6iane5ko9h30kribzgv/0 ",
"Subscribe_time": 1386160805
}

At this point, get the user's basic information.

This approach is most suitable for users in the attention of the time, reply to a welcome attention + user Nickname information, such as attention to the following public accounts in the reply shown. Scan two-dimensional code to experience.


The above is this article for the PHP Micro-trust public platform for the development of the user to obtain the basic information of the entire content, I hope you like.

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.