Home > Article > PHP Framework > How does ThinkPHP5 framework authorize WeChat public account web pages?
As WeChat public accounts become more and more popular, more and more people are starting to create their own public accounts. Among them, web page authorization is a common development method in public account development. This article will introduce how to use the ThinkPHP5 framework to authorize WeChat public account web pages.
1. Register a public account and obtain AppID and AppSecret
Before authorizing the WeChat public account web page, you first need to register a WeChat public account and apply for developer permissions. After the application is successful, you can obtain the two important parameters AppID and AppSecret in the "Developer Center".
2. Configure public account information
In the ThinkPHP5 framework, we can create a new wechat.php file in the config directory to store our public account configuration information. In this file, we need to configure the following information:
<?php return [ 'app_id' => 'your appid', 'app_secret' => 'your appsecret', 'auth_redirect' => 'your callback url', ];
Among them:
app_id
and app_secret
are what we manage in the official account Parameters obtained by the interface. auth_redirect
is the callback address after authorization of the WeChat webpage, and it must be a URL address accessible from the public network. 3. Obtain the web page authorization url
Before we call the WeChat web page authorization interface, we need to construct the web page authorization url. We can add the following code to the controller:
$config = config('wechat'); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $config['app_id'] . '&redirect_uri=' . urlencode($config['auth_redirect']) . '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
In the above code, we obtained the previously configured public account information through the config function, and constructed a URI authorized by the WeChat web page. Among them, response_type=code
means using code for authorization, scope=snsapi_userinfo
means the scope of authorization is to obtain basic user information.
4. Obtain the webpage authorization code
After constructing the webpage authorization URL, we need to jump to the URL for authorization. After the authorization is successful, the WeChat server will pass the code parameter back through GET. We can add the following code in the controller to get the code.
if (isset($_GET['code'])) { $code = $_GET['code']; } else { $this->redirect($url); }
In the above code, we first determine whether the URL contains the code parameter. If there is, it means that the user has been authorized successfully, and we will store the obtained code for subsequent use. If not, you need to jump and perform web page authorization.
5. Obtain user access_token and openId
After successful authorization, access_token and openId are required for subsequent operations. We can add the following code in the controller to obtain the user's access_token and openId.
$accessTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $config['app_id'] . '&secret=' . $config['app_secret'] . '&code=' . $code . '&grant_type=authorization_code'; $accessTokenResponse = json_decode(file_get_contents($accessTokenUrl), true); if (isset($accessTokenResponse['errcode'])) { throw new \Exception('ERROR ' . $accessTokenResponse['errcode'] . ': ' . $accessTokenResponse['errmsg']); } $accessToken = $accessTokenResponse['access_token']; $openId = $accessTokenResponse['openid'];
In the above code, we first constructed a URL requesting access_token, sent a request to the URL, and obtained the response result. If the response result contains errcode
, it means that there is an error in the request, and we will throw an exception; otherwise, we will store the obtained access_token and openId for subsequent use.
6. Obtain user detailed information
After obtaining the user's access_token and openId, we can obtain the user's detailed information through the following code:
$userInfoUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessToken . '&openid=' . $openId . '&lang=zh_CN'; $userInfoResponse = json_decode(file_get_contents($userInfoUrl), true); if (isset($userInfoResponse['errcode'])) { throw new \Exception('ERROR ' . $userInfoResponse['errcode'] . ': ' . $userInfoResponse['errmsg']); }
The above code , we constructed a URL that requested user information, sent a request to the URL, and obtained the response result. If the response contains errcode
, it means there is an error in the request and we will throw an exception.
At this point, we have successfully completed the process of authorizing the WeChat official account web page!
The above is the detailed content of How does ThinkPHP5 framework authorize WeChat public account web pages?. For more information, please follow other related articles on the PHP Chinese website!