Home > Backend Development > PHP Tutorial > PHP implements rapid push of WeChat template messages

PHP implements rapid push of WeChat template messages

墨辰丷
Release: 2023-03-27 18:02:01
Original
2965 people have browsed it

This article mainly introduces PHP to quickly push WeChat template messages. Friends who need it can refer to it.

You need to send template messages to following users. Since there are many public accounts following users, ordinary curl, etc. are used. Way too slow. Since template message sending does not need to wait for the result of WeChat, the fsockopen() function of PHP can be used to achieve the effect of rapid sending. The code is as follows:

$data = [
  'touser' => '11111111111111111',
  'template_id' => '111111111111111111',
  'url' => '11111111111111111111',
  'data' => [
    'first' => [
      'value' => '1111111111111111111',
      'color' => '#173177',
    ],
    'keyword1' => [
      'value' => '111111111111111111',
      'color' => '#173177',
    ],
    'keyword2' => [
      'value' => date('Y年m月d日 H:i'),
      'color' => '#173177',
    ],
    'remark' => [
      'value' => '1111111111111111111111111',
      'color' => '#173177',
    ]
  ]
];
$access_token = '此处填写自己公众号的access_token';
$params = json_encode($data,JSON_UNESCAPED_UNICODE);
$start_time = microtime(true);
for ($i = 0; $i < 50; $i++) {
  $fp = fsockopen(&#39;api.weixin.qq.com&#39;, 80, $error, $errstr, 1);
  $http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
  fwrite($fp, $http);
  fclose($fp);
}
print_r(microtime(true) - $start_time);
Copy after login

The above code sends 50 template messages. Please see the running results for the time:

0.83637619018555

You can also use curl to send template messages, or even curl's batch processing method (multi-threading), but the relatively faster method should be the above method.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPConnect to the database to implement addition, deletion, modification and query operations on the registration page_php skills

php $_SESSION member login instance sharing_php skills

##php ajax implementation of file upload progress bar_php skills

The above is the detailed content of PHP implements rapid push of WeChat template messages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template