<?php
namespace App\Libraries;
use PhpParser\Error;
/**
* 钉钉接口
* Class Dingtalk
* @package App\Libraries
*/
class Dingtalk
{
protected $bcw_webhook = '这里是创建的机器人接收消息地址';
/**
* 发送信息至钉钉群机器人
* @param $msg 要发送的信息
* @return false|mixed
*/
function sendMsgToDingTalk($msg)
{
$data = [
'msgtype' => 'text',
'text' => [
'content' => $msg,
]
];
$res = $this->doPost($this->bcw_webhook, $data);
$res = json_decode($res);
if($res) return $res;
return false;
}
private function doPost(string $url, array $param, $timeout = 30)
{
try {
$header[] = 'Content-Type: application/json';
$post_string = json_encode($param);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
$result = curl_exec($ch);
curl_close($ch);
} catch (Error $error) {
$result = false;
}
return $result;
}
}
调用
ding = new Dingtalk();
$change_desc = ‘’;
d
i
n
g
−
>
s
e
n
d
M
s
g
T
o
D
i
n
g
T
a
l
k
(
ding->sendMsgToDingTalk(
ding−>sendMsgToDingTalk(change_desc);
消息类型及数据格式
> 钉钉开放文档