遇到一个需求,用户注册后,要在钉钉通知销售主管。
第一步
新建钉钉群,以阿里云机器人为例子。 1.打开群设置 2.添加机器人后 点进机器人的个人资料 复制 access_token。 不太理解的话可以点击此处 阿里云Code机器人
Code
新建一个类 RobotService
<?php
class RobotService
{
private $dingdingWebHook = 'https://oapi.dingtalk.com/robot/send';
public function __construct()
{
$this->dingdingAccessToken = 'askdjhfkjahdjkfhajkshfkjahjkfahfhf'
}
public function sendNotice(array $data)
{
$dingdingAccessToken = 'askdjhfkjahdjkfhajkshfkjahjkfahfhf';
$data = [$content = "
### 注册用户信息
- 用户名称:王明显
- 手机号:16626267712
- 年龄:23
- 住址:上海市浦东新区
";
$message = [
'msgtype' => 'markdown',
'title' => '',
'markdown' => [
'title' => '注册用户信息',
'text' => $content,
],
'at' => [
'isAtAll' => true,
],
];
return $this->dingdingCurl(json_encode($data), $dingdingAccessToken);
}
private function dingdingCurl($postString, $dingdingAccessToken)
{
$dingdingWebHook = $this->dingdingWebHook . '?access_token=' . $dingdingAccessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dingdingWebHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json;charset=utf-8']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
三、效果图
|