企业微信机器人推送 php 写这个功能的实现
时间: 2024-03-20 13:41:49 浏览: 136
好的,下面是一个具体实现企业微信机器人推送的 PHP 代码示例:
```php
<?php
// 机器人 Webhook 地址和密钥
$webhookUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx';
$secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// 要发送的消息内容
$message = array(
'msgtype' => 'text',
'text' => array(
'content' => '这是一条测试消息'
)
);
// 生成签名
$timestamp = time();
$sign = urlencode(base64_encode(hash_hmac('sha256', $timestamp . "\n" . $secret . "\n" . json_encode($message), $secret, true)));
// 发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhookUrl . '×tamp=' . $timestamp . '&sign=' . $sign);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
// 解析响应结果
$json = json_decode($result, true);
if ($json['errcode'] == 0 && $json['errmsg'] == 'ok') {
echo '消息推送成功';
} else {
echo '消息推送失败:' . $json['errmsg'];
}
?>
```
以上代码中,$webhookUrl 和 $secret 分别是机器人的 Webhook 地址和密钥,$message 是要发送的消息内容,$timestamp 是当前时间戳,$sign 是使用 HMAC-SHA256 算法生成的签名。您需要将示例中的相关参数替换成您自己的参数,以实现机器人消息的推送。同时,根据实际需要,您也可以修改 $message 中的内容来发送不同类型的消息,如文本、图片、视频等。
阅读全文
相关推荐
















