How to obtain the user's OpenID on the WeChat public platform

Source: Internet
Author: User
Tags openid
This article mainly introduces how to obtain the user's OpenID on the public platform. developers must obtain an advanced interface after authorization to use this function. the user's OpenID is widely used in public platform construction, for more information about how to obtain the user's OpenID, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:

After you click the custom menu view type button, the client will open the url value (that is, the webpage link) filled in by the developer in the button to open the webpage, however, view cannot obtain the user's openid. you must use the "webpage authorization to obtain user Basic information" advanced interface to obtain the user's personal login information.

Specific method:

1. configure the webpage authorization callback domain name, such as www.bitsCN.com

2. simulate the third-party web page of public account, http://www.bitsCN.com/getcodeurl.php

<? Php if (isset ($ _ SESSION ['user']) {print_r ($ _ SESSION ['user']); exit ;} $ APPID = 'appid of the public account '; $ REDIRECT_URI = 'http: // response? Appid = '. $ APPID. '& redirect_uri = '. urlencode ($ REDIRECT_URI ). '& response_type = code & scope = '. $ scope. '& state = '. $ state. '# wechat_redirect'; header ("Location :". $ url);?>

3. in the redirection url of a third-party webpage, first obtain the code from the request, and then exchange openid and access_token based on the code, then, you can query User information based on the APIs called by openid and access_token.

<? Php // http://www.bitsCN.com/callback.php $ Appid = "appid of the public account"; $ secret = "app secret of the public account"; $ code = $ _ GET ["code"]; $ get_token_url =' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $ Appid. '& secret = '. $ secret. '& code = '. $ code. '& grant_type = authorization_code'; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ get_token_url); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10); $ res = curl_exec ($ ch); curl_close ($ ch); $ json_obj = json_decode ($ res, true); // query User information based on openid and access_token $ access_token = $ json_obj ['Access _ token']; $ openid = $ json_obj ['openid']; $ get_user_info_url =' https://api.weixin.qq.com/sns/userinfo?access_token= '. $ Access_token. '& openid = '. $ openid. '& lang = zh_CN'; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ get_user_info_url); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10); $ res = curl_exec ($ ch); curl_close ($ ch ); // parse json $ user_obj = json_decode ($ res, true); $ _ SESSION ['user'] = $ user_obj; print_r ($ user_obj);?>

I hope this article will help you develop php-based public platforms.

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.