Home  >  Article  >  Backend Development  >  Detailed steps to implement WeChat login function using PHP

Detailed steps to implement WeChat login function using PHP

PHPz
PHPzOriginal
2023-04-04 10:40:532571browse

In recent years, with the rise of social networks and the popularity of smartphones, WeChat has become an indispensable part of people's daily lives. In the field of Internet applications, implementing the WeChat login function is a very necessary part. As we all know, WeChat's authorization mechanism uses the OAuth 2.0 authorization mechanism, which brings great convenience to the implementation of our WeChat login function. Below we will introduce in detail how to implement the WeChat login function through PHP language.

1. WeChat development platform configuration

  1. Log in to [WeChat Open Platform](https://open.weixin.qq.com) to register an account. After registration is completed, enter WeChat Open Platform management center.
  2. Click the "Manage Official Account" menu on the center page and enter the WeChat official account information that needs to be accessed.
  3. After being authenticated by the WeChat Open Platform, we need to obtain the AppID and AppSecret of the WeChat Open Platform and record them in the login code. Log in to [WeChat Open Platform](https://open.weixin.qq.com), enter the management center, select "Mobile Application", and then select "Add Mobile Application".
  4. Fill in the basic information of the mobile application and submit it for review. After review and approval, you can obtain the AppID and AppSecret.

2. PHP code implementation

  1. Build WeChat login link
<?php
$appid = “your_appid”; //appid
$redirect_uri = urlencode(&#39;http://yourdomain.com/login.php&#39;); //登录成功回调网址,请确保此地址跟公众号设置的授权回调页面路径一致。
$scope = &#39;snsapi_userinfo&#39;; //snsapi_base 或 snsapi_userinfo
$url = &#39;https://open.weixin.qq.com/connect/oauth2/authorize?appid=&#39; . $appid . &#39;&redirect_uri=&#39; . $redirect_uri . &#39;&response_type=code&scope=&#39; . $scope . &#39;&state=STATE#wechat_redirect&#39;;
header(&#39;Location:&#39; . $url);
exit;
?>

In the above code, we need to fill in $appid, $redirect_uri and $scope parameter. Among them, $appid is the AppID assigned to us by the WeChat open platform; $redirect_uri is the callback URL after user authorization, which needs to be consistent with the authorization callback page set by the official account; $scope is divided into snsapi_base and snsapi_userinfo, the former can only obtain the user's openid , and the latter can obtain the user's basic information.

  1. Get access_token and openid
<?php
$appid = &#39;your_appid&#39;; //appid
$secret = &#39;your_appsecret&#39;; //appsecret
$code = $_GET[&#39;code&#39;]; //网页授权code
$access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39; . $appid . &#39;&secret=&#39; . $secret . &#39;&code=&#39; . $code . &#39;&grant_type=authorization_code&#39;; //获取access_token和openid的链接
$access_token = file_get_contents($access_token_url);
$access_token_arr = json_decode($access_token, true); //将返回的json字符串转为数组
?>

In this code, we pass the code returned by the user after successful authorization, and then pass the code to the WeChat server to obtain the access_token and openid.

  1. Get the user’s basic information
<?php
$access_token = $access_token_arr[&#39;access_token&#39;];
$openid = $access_token_arr[&#39;openid&#39;];
$user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39; . $access_token . &#39;&openid=&#39; . $openid . &#39;&lang=zh_CN&#39;; //获取用户信息的链接
$user_info = file_get_contents($user_info_url);
$user_info_arr = json_decode($user_info, true); //将返回的json字符串转为数组
?>

In this code, we use access_token and openid to obtain the user’s basic information, such as user nickname, gender, city, etc. It should be noted that before obtaining the user's basic information, we need to ensure that the user has authorized the scope to snsapi_userinfo.

  1. Complete login example code
<?php
if (!isset($_GET[&#39;code&#39;]) || empty($_GET[&#39;code&#39;])) {
    //第一步:用户同意授权,获取code
    $appid = &#39;your_appid&#39;;
    $redirect_uri = urlencode(&#39;http://yourdomain.com/login.php&#39;);
    $scope = &#39;snsapi_userinfo&#39;;
    $url = &#39;https://open.weixin.qq.com/connect/oauth2/authorize?appid=&#39; . $appid . &#39;&redirect_uri=&#39; . $redirect_uri . &#39;&response_type=code&scope=&#39; . $scope . &#39;&state=STATE#wechat_redirect&#39;;
    header(&#39;Location:&#39; . $url);
    exit;
} else {
    //第二步:通过code换取网页授权access_token以及openid,再获取用户信息
    $appid = &#39;your_appid&#39;;
    $secret = &#39;your_appsecret&#39;;
    $code = $_GET[&#39;code&#39;];
    $access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39; . $appid . &#39;&secret=&#39; . $secret . &#39;&code=&#39; . $code . &#39;&grant_type=authorization_code&#39;;
    $access_token = file_get_contents($access_token_url);
    $access_token_arr = json_decode($access_token, true);
    $access_token = $access_token_arr[&#39;access_token&#39;];
    $openid = $access_token_arr[&#39;openid&#39;];
    $user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39; . $access_token . &#39;&openid=&#39; . $openid . &#39;&lang=zh_CN&#39;;
    $user_info = file_get_contents($user_info_url);
    $user_info_arr = json_decode($user_info, true);

    //TODO:在这里可以将用户信息存入数据库,供之后使用
    //......
}
?>

3. Summary

As mentioned above, in a few simple steps, we can use PHP language to Implement WeChat login function. This article only introduces the most basic WeChat login implementation method. In actual application, there are more issues that need attention, such as the judgment of user permissions, the validity period of authorization, etc. I hope this article can provide some help to developers who need to implement WeChat login.

The above is the detailed content of Detailed steps to implement WeChat login function using PHP. For more information, please follow other related articles on the PHP Chinese website!

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