自定义机器人的功能很不错,可以自定义我们需要发送的信息,例如服务器报警信息等,单官方文档示例还是存在一些问题,示例代码:
<?php
function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$webhook = "https://oapi.dingtalk.com/robot/send?access_token=d02cbcf5b22e4a8c8cc733f2a543327243825c89b0cef0b314f7105f010f1289";
$message="你好牛逼啊";
$data = array ('msgtype' => 'text','text' => array ('content' => $message));
$data_string = json_encode($data);
$result = request_by_curl($webhook, $data_string);
echo $result;
由于请求的链接是HTTPS,所以需要加上curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);,否则就会请求失败了