Php WeChat public platform development-getting basic user information-php instances

Source: Internet
Author: User
Tags openid
This article describes how to obtain basic information about users on the public platform, including nicknames, portraits, and gender information. The following small series will share with you the latest articles on php public platform development and getting basic user information, if you have any need, you can refer to the methods in this article to include subscription numbers, service numbers, and custom menus. Whether you have advanced interface permissions or not, you can obtain basic user information, you do not need to simulate logon.

It is a common solution for many customers to integrate with third-party websites. This section briefly describes how to obtain basic user information and directly log on to a third-party website.

When a public account pays attention to a public account, it generates a unique OpenID. In this case, we need to use it to request the server to obtain basic user information, including profile pictures and nicknames.

For more details, see the development documentation.

The procedure is as follows:

1 Step 1: The user agrees to the authorization and obtains the code
2 Step 2: Exchange code for webpage authorization access_token
3 Step 3: refresh access_token (if needed)
4. Step 4: Pull user information (the scope must be snsapi_userinfo)
5 Appendix: Check whether the authorization credential (access_token) is valid

1. On the public platform-Developer Center: webpage authorization to obtain basic user information: click "modify" to configure the webpage callback Domain Name

2. user authorization code

Required resources:

$ Appid = '*****************';
$ Appsecret = '*************************';

Note: redirect_url is the callback link address for redirection after authorization. Use urlencode to process the link.

Add 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 an appeal url.

After authorization, the user calls back the domain name and Concatenates the required code in the url parameter. We can GET it directly with $ _ GET ['code!

3. Get the webpage authorization access_token and openid through code

$ 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;

4. obtain user information

$ Info_url = 'https: // api.weixin.qq.com/sns/userinfo? Access_token = '. $ access_token.' & openid = '. $ opendid.' token = zh_CN ';
$ Info = json_decode (file_get_contents ($ info_url ));
$ Data ['name'] = $ info-> nickname;
$ Data ['image'] = $ info-> headimgurl;
Print_r ($ info );

5. After obtaining the user's openid and other information, we can save it to the database. As long as there is an openid, It is equivalent that the user has logged on to the website!

Use the access_token obtained by AppID and AppSecret to obtain basic user information through global Access Token.

1. You can obtain your OpenID when following and replying to a message.

  
  gh_b629c48b653e
   
  ollB4jv7LA3tydjviJp5V9qTU_kA
   
  
   1372307736
   
  event
   
  subscribe
   
  
  
 

FromUserName indicates the OpenID.

2. Use the access_token interface to obtain the global Access Token.

Https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET
Returned results:

{ "access_token": "NU7Kr6v9L9TQaqm5NE3OTPctTZx797Wxw4Snd2WL2HHBqLCiXlDVOw2l-Se0I-WmOLLniAYLAwzhbYhXNjbLc_KAA092cxkmpj5FpuqNO0IL7bB0Exz5s5qC9Umypy-rz2y441W9qgfnmNtIZWSjSQ", "expires_in": 7200}

3. Use global ACCESS_TOKEN to obtain detailed information about OpenID.

Https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ACCESS_TOKEN & openid = OPENID
The returned result is as follows:

{
"Subscribe": 1,
"Openid": "oLVPpjqs2BhvzwPj5A-vTYAX4GLc ",
"Nickname": "baby hedgehog ",
"Sex": 1,
"Language": "zh_CN ",
"City": "Shenzhen ",
"Province": "Guangdong ",
"Country": "China ",
"Headimgurl": "http://wx.qlogo.cn/mmopen/JcDicrZBlREhnNXZRudod9PmibRkIs5K2f1tUQ7lFjC63pYHaXGxNDgMzjGDEuvzYZbFOqtUXaxSdoZG6iane5ko9H30krIbzGv/0 ",
"Subscribe_time": 1386160805
}

Now, obtain the basic information of the user.

This method is most suitable for users to reply to a message that welcomes attention + user nickname when paying attention, as shown in the response when paying attention to the following public account. Scan the QR code to try it out.


The above is all the content of this article for php public platform development to obtain basic user information. I hope you will like it.

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.