官方文档:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
    本文参考来源:https://www.jianshu.com/p/cc42a7915acc

    1. <?php
    2. /**
    3. * php 使用钉钉机器人发送消息.
    4. * User: Administrator
    5. * Date: 2019/8/29
    6. * Time: 23:40
    7. */
    8. function request_by_curl($remote_server, $post_string)
    9. {
    10. $ch = curl_init();
    11. curl_setopt($ch, CURLOPT_URL, $remote_server);
    12. curl_setopt($ch, CURLOPT_POST, 1);
    13. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    14. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
    15. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    17. // 不用开启curl证书验证
    18. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    19. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    20. $data = curl_exec($ch);
    21. //$info = curl_getinfo($ch);
    22. //var_dump($info);
    23. curl_close($ch);
    24. return $data;
    25. }
    26. $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
    27. // text类型
    28. $textString = json_encode([
    29. 'msgtype' => 'text',
    30. 'text' => [
    31. "content" => "我就是我, 是不一样的烟火@156xxxx8827"
    32. ],
    33. 'at' => [
    34. 'atMobiles' => [
    35. "156xxxx8827",
    36. "189xxxx8325"
    37. ],
    38. 'isAtAll' => false
    39. ]
    40. ]);
    41. // link类型
    42. $textString = json_encode([
    43. "msgtype" => "link",
    44. "link" => [
    45. "text" => "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。
    46. 而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
    47. "title" => "时代的火车向前开",
    48. "picUrl" => "",
    49. "messageUrl" => "https://www.dingtalk.com/",
    50. ]
    51. ]);
    52. // markdown类型
    53. $textString = json_encode([
    54. "msgtype" => "markdown",
    55. "markdown" => [
    56. "title" => "杭州天气",
    57. "text" => "#### 杭州天气 @156xxxx8827\n" .
    58. "> 9度,西北风1级,空气良89,相对温度73%\n\n" .
    59. "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" .
    60. "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
    61. ],
    62. "at" => [
    63. "atMobiles" => [
    64. "156xxxx8827",
    65. "189xxxx8325"
    66. ],
    67. "isAtAll" => false
    68. ]
    69. ]);
    70. // 整体跳转ActionCard类型
    71. $textString = json_encode([
    72. "actionCard" => [
    73. "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
    74. "text" => "![screenshot](@lADOpwk3K80C0M0FoA)
    75. ### 乔布斯 20 年前想打造的苹果咖啡厅
    76. Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
    77. "hideAvatar" => "0",
    78. "btnOrientation" => "0",
    79. "singleTitle" => "阅读全文",
    80. "singleURL" => "https://www.dingtalk.com/"
    81. ],
    82. "msgtype" => "actionCard"
    83. ]);
    84. // 独立跳转ActionCard类型
    85. $textString = json_encode([
    86. "actionCard" => [
    87. "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
    88. "text" => "![screenshot](@lADOpwk3K80C0M0FoA)
    89. ### 乔布斯 20 年前想打造的苹果咖啡厅
    90. Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
    91. "hideAvatar" => "0",
    92. "btnOrientation" => "0",
    93. "btns" => [
    94. [
    95. "title" => "内容不错",
    96. "actionURL" => "https://www.dingtalk.com/"
    97. ],
    98. [
    99. "title" => "不感兴趣",
    100. "actionURL" => "https://www.dingtalk.com/"
    101. ]
    102. ]
    103. ],
    104. "msgtype" => "actionCard"
    105. ]);
    106. // FeedCard类型
    107. $textString = json_encode([
    108. "feedCard" => [
    109. "links" => [
    110. [
    111. "title" => "时代的火车向前开1",
    112. "messageURL" => "https://www.dingtalk.com/",
    113. "picURL" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567105217584&di=4c91fefc045f54267edcf8c544e3bd3b&imgtype=0&src=http%3A%2F%2Fk.zol-img.com.cn%2Fdcbbs%2F16420%2Fa16419096_s.jpg"
    114. ],
    115. [
    116. "title" => "时代的火车向前开2",
    117. "messageURL" => "https://www.dingtalk.com/",
    118. "picURL" => ""
    119. ]
    120. ]
    121. ],
    122. "msgtype" => "feedCard"
    123. ]);
    124. $result = request_by_curl($webhook, $textString);
    125. echo $result;

    以上是使用的第一种方式,关键词,要想发送成功,文本内容必须得包含该关键词


    若使用签名的方式,签名算法相关的如下所示,参考来源:https://blog.csdn.net/qq_34193883/article/details/106471299?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-3-106471299.nonecase

    1. <?php
    2. $url = 'webhook地址';
    3. // 第一步,把timestamp+"\n"+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)。
    4. $time = time() *1000;//毫秒级时间戳,我这里为了方便,直接把时间*1000了
    5. $secret = '这是密钥';
    6. $sign = hash_hmac('sha256', $time . "\n" . $secret,$secret,true);
    7. $sign = base64_encode($sign);
    8. $sign = urlencode($sign);
    9. $msg = [
    10. 'msgtype' => 'text',//这是文件发送类型,可以根据需求调整
    11. 'text' => [
    12. 'content' => '这是需要发送的内容',
    13. ],
    14. ];
    15. $url = "{$url}&timestamp={$time}&sign={$sign}";
    16. $curl = curl_init();
    17. curl_setopt($curl, CURLOPT_URL, $url);
    18. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    19. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    20. curl_setopt($curl, CURLOPT_POST, 1);
    21. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($msg));
    22. curl_setopt($curl, CURLOPT_HEADER, 0);
    23. curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    24. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    25. $res = curl_exec($curl);
    26. curl_close($curl);
    27. var_dump($res);