With the growing popularity of WeChat Official Accounts, more and more businesses and individuals want to interact with users through their accounts. The bulk messaging feature is particularly important to improve the efficiency of information transmission. This article explains how to implement bulk messaging for WeChat Official Accounts using PHP, and provides detailed code examples.
Before you begin writing the code, ensure you have the following conditions in place:
Next, create a PHP file named send_message.php and add the following code:
<?php // Define the App ID and App Secret of the WeChat Official Account $appId = 'your_app_id'; $appSecret = 'your_app_secret'; // Define the message content to be sent $message = 'Hello, World!'; // Get the AccessToken $accessToken = getAccessToken($appId, $appSecret); // Construct the URL for sending the bulk message $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=' . $accessToken; // Construct the message data $data = array( 'filter' => array( 'is_to_all' => true // Send to all users ), 'text' => array( 'content' => $message // Message content ), 'msgtype' => 'text' // Message type is text ); // Send the bulk message $result = httpPost($url, json_encode($data)); // Output the result echo $result; // Function to get the AccessToken function getAccessToken($appId, $appSecret) { $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret; $result = file_get_contents($url); $resultJson = json_decode($result, true); return $resultJson['access_token']; } // Function to send HTTP POST request function httpPost($url, $data) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' )); $result = curl_exec($ch); curl_close($ch); return $result; } ?>
Save and upload the send_message.php file to your server, then access the URL of the file to trigger the bulk message sending.
It is important to note that the bulk messaging feature in WeChat Official Accounts has certain conditions. The account must meet certain requirements such as follower count and authentication status to use this feature. Please refer to the WeChat Official Account platform documentation for detailed restrictions.
This article explained how to implement bulk messaging for WeChat Official Accounts using PHP and provided a specific code example. With this code, developers can easily send messages to a large number of users at once, improving the efficiency of information transmission. In practice, you can modify and extend the functionality based on your needs. I hope this article helps developers who want to use PHP to implement bulk messaging for their WeChat Official Accounts.