Current Location: Home> Latest Articles> Complete Guide to Implementing WeChat H5 Silent Login with PHP

Complete Guide to Implementing WeChat H5 Silent Login with PHP

M66 2025-11-05

Basic Requirements for WeChat H5 Silent Login

When developing a WeChat H5 page with PHP, you need to meet the following requirements to enable silent login functionality:

  • Register a business domain: You must register your business domain on the WeChat Open Platform for web authorization and user authentication.
  • Call the WeChat Web Authorization API: Use WeChat’s web authorization API to obtain an authorization code (code) for silent login.
  • Obtain the WeChat Authorization Code: When a user visits your H5 page and is logged into WeChat, the system returns an authorization code.
  • Configure a WeChat Mini Program AppID: Register a mini program on the WeChat Open Platform and obtain its AppID for API requests.

Implementation Steps for WeChat H5 Silent Login

Here’s how to implement WeChat H5 silent login in a PHP environment:

  • Register a business domain: Go to the WeChat Open Platform (https://open.weixin.qq.com), submit your domain details and usage information, and wait for approval.
  • Call the WeChat Web Authorization API: On your business domain page, call the WeChat web authorization interface (such as wx.login) to obtain the code value for silent login verification.
  • Obtain the authorization code: After user authorization, WeChat returns a code used to exchange for an access token.
  • Request the access token using the Mini Program AppID: Send a request from PHP to the WeChat server using the code and AppID to get the access token and user’s unique identifier (unionid).
// Example: Get access_token via code
$code = $_GET['code'];
$appId = 'yourAppID';
$appSecret = 'yourAppSecret';
$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);
$data = json_decode($response, true);
$access_token = $data['access_token'];
$openid = $data['openid'];
  • Retrieve user information: Once you have the access token, call the WeChat API to fetch the user’s basic information.
// Example: Get user info
$userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$userinfo = file_get_contents($userinfo_url);
$userinfo = json_decode($userinfo, true);

Important Notes on Silent Login

  • Silent login is only applicable to users already logged into the WeChat client.
  • WeChat imposes request frequency limits, so avoid excessive requests.
  • It’s recommended to review the WeChat Open Platform documentation for detailed authorization rules and interface constraints.

Conclusion

By following these steps, developers can implement WeChat H5 silent login in PHP projects, allowing secure user authentication and data exchange without interrupting the user experience.