1. Configure the domain name of the authorization callback page, such as www.aaa.com
2. Simulate the third-party webpage of the official account, fn_system.php
Php code
-
-
- if(emptyempty($_SESSION['user'])) {
-
- header ("Location:http://www.aaa.com/uc/fn_wx_login.php"); [
- 'user']);
} - ?>
- 3. When accessing a third-party web page, if there is no session information in the session , then jump Go to the login page, fn_wx_login.phpPhp code
=
"Appid of official account in WeChat" ; -
$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); ?>
5. On the user authorization page of WeChat, if the user selects "Agree Authorization", the code parameter will be attached when WeChat jumps back to the bounce address of the third-party web page. - 6. In the bounce URL of the third-party web page, first obtain the code from the request, and then exchange it for openid and access_token based on the code. Then you can call the relevant interface of WeChat based on openid and access_token to query user information. Php code
-
- $appid = "appid of the official account in WeChat";
- $secret = " The official account is in the WeChat app secret"; $get_token_url
- = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret
- . '&code='.$code.'&grant_type=authorization_code'; 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); //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 $res,true);
- $ _SESSION['user'] =
- $user_obj;
print_r(- $user_obj);
- ?>
The above has introduced the WeChat public platform OAuth20 web page authorization PHP example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
-
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn