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
2. PHP code implementation
<?php $appid = “your_appid”; //appid $redirect_uri = urlencode('http://yourdomain.com/login.php'); //登录成功回调网址,请确保此地址跟公众号设置的授权回调页面路径一致。 $scope = 'snsapi_userinfo'; //snsapi_base 或 snsapi_userinfo $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=' . $scope . '&state=STATE#wechat_redirect'; header('Location:' . $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.
<?php $appid = 'your_appid'; //appid $secret = 'your_appsecret'; //appsecret $code = $_GET['code']; //网页授权code $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code'; //获取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.
<?php $access_token = $access_token_arr['access_token']; $openid = $access_token_arr['openid']; $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; //获取用户信息的链接 $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.
<?php if (!isset($_GET['code']) || empty($_GET['code'])) { //第一步:用户同意授权,获取code $appid = 'your_appid'; $redirect_uri = urlencode('http://yourdomain.com/login.php'); $scope = 'snsapi_userinfo'; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=' . $scope . '&state=STATE#wechat_redirect'; header('Location:' . $url); exit; } else { //第二步:通过code换取网页授权access_token以及openid,再获取用户信息 $appid = 'your_appid'; $secret = 'your_appsecret'; $code = $_GET['code']; $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code'; $access_token = file_get_contents($access_token_url); $access_token_arr = json_decode($access_token, true); $access_token = $access_token_arr['access_token']; $openid = $access_token_arr['openid']; $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; $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!