Current Location: Home> Latest Articles> Complete Guide to Implementing WeChat Mini Program Projection Function with PHP

Complete Guide to Implementing WeChat Mini Program Projection Function with PHP

M66 2025-10-26

Understanding the WeChat Mini Program Projection Feature with PHP

As WeChat Mini Programs continue to evolve, more developers and businesses are looking to integrate advanced features into their applications. One useful feature is the projection function, which allows users to project content from the Mini Program onto a larger external display. This tutorial will show you how to implement the projection feature in a WeChat Mini Program using PHP, complete with a working code example.

Preparation Before Development

Before starting, make sure you meet the following requirements:

  • Basic understanding of PHP programming;
  • Familiarity with WeChat Mini Program development and its API structure;
  • A working PHP environment on your server;
  • Valid WeChat Mini Program AppID and AppSecret.

Once these are in place, you can proceed to use PHP to interact with the WeChat API and enable the projection feature.

Getting the access_token

Before calling any WeChat API, you must obtain an access_token. This token is essential for authenticating your requests to the WeChat server.

Here’s a PHP example of how to retrieve the access_token:

<?php
  $appId = "YourAppID";
  $appSecret = "YourAppSecret";
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
  $result = file_get_contents($url);
  $result = json_decode($result, true);
  $access_token = $result["access_token"];
?>

This PHP script sends a request to the WeChat server to get the access_token and decodes the returned JSON response to extract the token value.

Using PHP to Call the Projection API

Once you have the access_token, you can call the WeChat API to activate the Mini Program projection feature.

<?php
  $appId = "YourAppID";
  $openId = "UserOpenID";
  $access_token = "YourAccessToken";
  $content = "Content to be projected";
  
  $url = "https://api.weixin.qq.com/wxa/devplugin?access_token=".$access_token;
  $data = array(
      "action" => "open",
      "plugin_appid" => $appId,
      "openid" => $openId,
      "content" => $content
  );
  $data = json_encode($data);
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($curl);
  curl_close($curl);
  
  $result = json_decode($result, true);
  if ($result["errmsg"] == "ok") {
      echo "Projection successful!";
  } else {
      echo "Projection failed!";
  }
?>

In this code:

  • $appId is your Mini Program’s AppID;
  • $openId represents the user’s OpenID;
  • $access_token is the token retrieved earlier;
  • $content is the data to be projected.

If executed successfully, this code will trigger the projection feature within your Mini Program.

Common Issues and Optimization Tips

During development, you may encounter some issues such as:

  • Expired access_token: The WeChat access_token usually expires in 2 hours. Implement a cache mechanism to refresh it automatically.
  • API call failure: Check if your access_token and openid values are correct.
  • Network errors: Ensure your server can access WeChat’s API endpoints properly.

It’s recommended to create a dedicated function or class for managing the access_token to improve reliability and performance.

Conclusion

By following the steps above, you can easily implement the WeChat Mini Program projection feature using PHP. The process involves:

  • Retrieving the access_token;
  • Calling the WeChat projection API via PHP;
  • Handling and verifying the response.

With this foundation, developers can expand the feature further — such as dynamic content projection or real-time interaction — to enhance user experience and make the Mini Program more engaging.