Home  >  Article  >  Backend Development  >  How to implement WeChat login function in php

How to implement WeChat login function in php

PHPz
PHPzOriginal
2023-04-24 10:53:121566browse

As a popular web development language, PHP provides developers with many powerful tools and libraries, making it easier to integrate with other platforms. Nowadays, many websites have adopted the WeChat login function, allowing users to easily log in using their WeChat account. So, how do we implement this function in PHP? This article will introduce you to some implementation methods.

  1. Get WeChat developer account and key

Before we start, we need to obtain WeChat developer account and key. First log in to the WeChat public platform, then under the "Development" tab, click the "Webpage Authorization to Obtain Basic User Information" button of "Interface Permissions", and finally complete the authorization according to the prompts to obtain the developer account and key.

  1. Build the login link

Once we have the WeChat developer account and key, we can use PHP to build the login link. When building a link, we need to use the OAuth2.0 protocol provided by WeChat to request user authorization from WeChat and obtain the user's openID (the user's unique identifier within the official account).

For example, the following is a sample link:

https://open.weixin.qq.com/connect/oauth2/authorize?
appid=APPID&
redirect_uri=REDIRECT_URI&
response_type=code&
scope=snsapi_userinfo&
state=STATE#wechat_redirect

Among them, replace APPID and REDIRECT_URI with our WeChat developer account and URL.

  1. Get user information

After obtaining user authorization through step 2, we can obtain user information by calling the API provided by WeChat. Specifically, we can use the following code to obtain user information from the WeChat server:

// 获取access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
$response = file_get_contents($url);
$result = json_decode($response, true);

// 获取用户信息
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$result['access_token']."&openid=".$result['openid']."&lang=zh_CN";
$response = file_get_contents($url);
$userInfo = json_decode($response, true);

In this code, we first obtain the user's access_token and openID by calling the "access_token" interface provided by WeChat. Then, we call the "userinfo" interface provided by WeChat again and pass the access_token and openID parameters to obtain the user's detailed information.

  1. Implement WeChat login

Finally, we can query whether the user exists from our database based on the obtained user information. If it is a new user, add it to our database and set up a user session so that user information can be saved and continuous login can be achieved during future user visits.

The following is a sample implementation:

if ($userInfo) {
  $query = "SELECT * FROM users WHERE openid = '".$userInfo['openid']."'";
  $result = mysqli_query($conn, $query);

  if (mysqli_num_rows($result) == 0) {
    $query = "INSERT INTO users (openid, nickname, sex, province, city, country, headimgurl, created_at) VALUES ('".$userInfo['openid']."', '".$userInfo['nickname']."', '".$userInfo['sex']."', '".$userInfo['province']."', '".$userInfo['city']."', '".$userInfo['country']."', '".$userInfo['headimgurl']."', NOW())";
    mysqli_query($conn, $query);
  }

  $_SESSION['openid'] = $userInfo['openid'];
  $_SESSION['nickname'] = $userInfo['nickname'];
  $_SESSION['headimgurl'] = $userInfo['headimgurl'];

  // 将用户重定向到您网站的一个页面
  header('Location: /index.php');
}

In this code, we first use the query statement to find whether the user exists in the database. If it does not exist, add it to the database using an insert statement. Finally, we set up the user session so that the user's information persists after they log in and redirects them to a page on our site.

Summary

Through the above steps, we can easily implement the WeChat login function in PHP. It should be noted that when implementing this function, developers need to fully comply with the standards provided in the WeChat developer documentation and properly process user information obtained from the WeChat server.

The above is the detailed content of How to implement WeChat login function in 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