class
<?php
define('WEBHOOK_URL', "https://oapi.dingtalk.com/robot/send?access_token=%s×tamp=%d&sign=%s");
class Dingtalk
{
private $config;
public function __construct($access_token = '', $secret = '')
{
if (!empty($access_token) && !empty($secret)) {
$this->config = array(
"token" => $access_token,
"secret" => $secret
);
} else {
$this->config = array(
"token" => "your robot access_token",
"secret" => "your robot secret"
);
}
}
public function send($msgtype = 'text', $data = array(), $isAtAll = false, $atMobiles = array())
{
if (empty($data)) {
return "msgtype can't be empty.";
}
$post = array(
"msgtype" => $msgtype,
$msgtype => $data,
"at" => array(
"isAtAll" => $isAtAll,
"atMobiles" => $atMobiles
)
);
$post_string = json_encode($post);
return $this->request_by_curl($post_string);
}
private function request_by_curl($post_string)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getUrl());
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
return curl_getinfo($ch);
}
curl_close($ch);
return $data;
}
private function getUrl()
{
$time = time() * 1000;
$str = $time . "\n" . $this->config['secret'];
$sign = urlencode(base64_encode(hash_hmac('sha256', $str, $this->config['secret'], true)));
return sprintf(WEBHOOK_URL, $this->config['token'], $time, $sign);
}
public function sendText($msg, $atAll = false, $atMobiles = array())
{
return $this->send("text", array("content" => $msg), $atAll, $atMobiles);
}
}
文本消息
$ding = new Dingtalk();
$ding -> sendText("用户A登录了后台"); #基础
$ding -> sendText("用户A登录了后台", true); # at所有人
$ding -> sendText("用户A登录了后台", false, array(15210000000)); # at单人
其他消息可自行开发
$ding = new Dingtalk();
#link
$linkData = array (
'text' => "测试",
"title"=>"11111",
"picUrl"=>"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2985114338,3296556723&fm=58&bpow=640&bpoh=820",
"messageUrl"=>"https://baike.baidu.com/item/%E4%BD%9F%E4%B8%BD%E5%A8%85/2794873?fr=aladdin"
);
$ding->send("link", $linkData);
#feedCard
feedCardData = array (
"links"=>array(
array(
"title"=>"佟丽娅",
"messageURL"=>"https://baike.baidu.com/item/%E4%BD%9F%E4%B8%BD%E5%A8%85/2794873?fr=aladdin",
"picURL"=>"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2985114338,3296556723&fm=58&bpow=640&bpoh=820"
) ,
array(
"title"=>"杨幂",
"messageURL"=>"https://baike.baidu.com/item/%E6%9D%A8%E5%B9%82/149851?fr=aladdin","picURL"=>"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2328873263,1601203834&fm=179&app=42&f=JPEG?w=121&h=140"
) ,
array(
"title"=>"陈乔恩",
"messageURL"=>"https://baike.baidu.com/item/%E9%99%88%E4%B9%94%E6%81%A9/219495?fr=aladdin",
"picURL"=>"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1131495323,1157171667&fm=27&gp=0.jpg"
)
)
);
$ding->send("feedCard", $feedCardData);
|