PHP quickly pushes WeChat template messages

不言
Release: 2023-03-24 18:10:01
Original
2347 people have browsed it

The content of this article is about PHP quickly pushing WeChat template messages. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Original address : https://blog.csdn.net/wanlinzan/article/details/70171782

It is necessary to send template messages to following users. Since there are many following users on public accounts, so Using ordinary curl and other methods is 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 sent 50 template messages. Please see the running results for the time:

0.83637619018555
Copy after login

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

##Original address: https://blog.csdn.net/wanlinzan/article/details/70171782


The above is the detailed content of PHP quickly pushes 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