php对接钉钉自定义机器人

技术 置顶 精帖
0 850
peng49
peng49 2021-11-17 14:12:18
 

钉钉自定义机器人接入

以安全设置选择加签为例

  1. $timestamp = time() * 1000;
  2. //签名秘钥(添加机器人时可查看保存)
  3. $secret = 'SECxxxxxxxxxxxxxx';
  4. $sign = base64_encode(hash_hmac('sha256', "{$timestamp}\n{$secret}", $secret, true));
  5. $query = http_build_query(['timestamp' => $timestamp, 'sign' => $sign]);
  6. $url = "https://oapi.dingtalk.com/robot/send?access_token=xxx&" . $query;
  7. //消息内容
  8. $message = [
  9. 'msgtype' => 'text',
  10. 'text' => [
  11. 'content' => 'TEST'
  12. ],
  13. 'at' => [
  14. 'atMobiles' => ['手机号']
  15. ]
  16. ];
  17. $header = [
  18. 'Content-Type: application/json'
  19. ];
  20. //发送请求
  21. $resource = curl_init($url);
  22. curl_setopt($resource, CURLOPT_POST, true);
  23. curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
  24. curl_setopt($resource, CURLOPT_POSTFIELDS, json_encode($message));
  25. curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
  26. curl_setopt($resource, CURLOPT_SSL_VERIFYPEER, false);
  27. curl_setopt($resource, CURLOPT_SSL_VERIFYHOST, false);
  28. $resp = curl_exec($resource);
  29. curl_close($resource);
  30. var_dump($resp);
回帖
登录
忘记密码?