1, configure the authorization callback page domain name, such as Www.aaa.com
2. Third party Web page of The Sims, fn_system.php
PHP Code
-
- if (emptyempty($_session[' user ']) ){
- Header ("location:http://www.aaa.com/uc/fn_wx_login.php");
- }Else{
- Print_r ($_session[' user ']);
- }
- ?>
3, when visiting the third party webpage, if there is no session information in the check session, jump to the landing page, fn_wx_login.php
PHP Code
-
- $appid = "The AppID of the public number" ;
- $url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= ' . $appid . ' &redirect_uri=http%3a%2f%2fwww.aaa.com%2fuc%2ffn_callback.php&resp/span>;
- Header ("Location:". ) $url );
- ?>
4, in the landing page assembly AppID, bounce URL and other information, and then jump to the user authorization page.
5, on the user authorization page, if the user selected "Consent authorization", then back to the third party Web page to jump back to the address, will be accompanied by the code parameter.
6, the Third party Web page bounce URL, first get code from the request, and then according to code further in exchange for OpenID and Access_token, and then can be based on the OpenID and Access_token invoke the relevant interface to query user information.
PHP Code
-
- $appid = "The AppID of the public number" ;
- $secret = "The app secret in the public number" ;
- $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, ten);
- $res = curl_exec ($ch);
- Curl_close ($ch);
- $json _obj = Json_decode ($res, true);
- //Search for 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, ten);
- $res = curl_exec ($ch);
- Curl_close ($ch);
- //Parse JSON
- $user _obj = Json_decode ($res, true);
- $_session [' user '] = $user _obj ;
- Print_r ($user _obj);
- ?>
The above describes the public platform OAUTH20 Web page authorization PHP example, including the content, I hope the PHP tutorial interested in a friend helpful.