The public platform implements the method of obtaining the user's OpenID. The public openid
This document describes how the public platform can obtain the user's OpenID. 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.jb51.net
2. Simulate the third-party web page of Public Account, http://www.bkjia.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.bkjia.com/callback.php?appid = "public account in appid"; $ secret = "public account in app secret"; $ 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_o Bj ['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.