本文主要讲述的是如何通过签名方式实现钉钉机器人报警的功能:

1、关于签名的生成:

  /**
   * 签名实现
   */
  list($s1, $s2) = explode(' ', microtime());
  $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
  $secret = '****';
  $data = $timestamp . "\n" . $secret;
  $signstr = base64_encode(hash_hmac('sha256', $data, $secret,true));
  $signstr = utf8_encode(urlencode($signstr));
  $webhook = 'https://oapi.dingtalk.com/robot/send?access_token=****';
  $webhook .= "&timestamp=$timestamp&sign=$signstr";

2:使用guzzle请求接口,发送钉钉消息

  /**
   * 发送钉钉报警
   */
  $guzzleclent = new \guzzlehttp\client();

  $data = [
    'msgtype' => 'text',
    'text' => [
      'content' => $msg,
    ]
  ];

  $res = $guzzleclent->request('post', $webhook,[
    'headers' => [
      'content-type' => 'application/json'
    ],
    'body' => json_encode($data),
  ]);
  $res = json_decode($res->getbody());
  var_dump($res);

到此这篇关于php封装实现钉钉机器人报警接口的示例代码的文章就介绍到这了,更多相关php 钉钉机器人报警接口内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!