Home > PHP Framework > ThinkPHP > body text

How does ThinkPHP5 framework authorize WeChat public account web pages?

PHPz
Release: 2023-04-14 11:22:58
Original
1465 people have browsed it

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:

 'your appid',
    'app_secret' => 'your appsecret',
    'auth_redirect' => 'your callback url',
];
Copy after login

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';
Copy after login

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);
}
Copy after login

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'];
Copy after login

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']);
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!