开发者社区> 问答> 正文

钉钉自定义机器人PHP版如何接入

如以下代码,怎么木有任何反应,也木有任何返回值,是什么鬼,有前辈能指教下么



<?php$urls="https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXX";
$messages="测试机器人吧";$data = array ('msgtype' => 'text','text' => array ('content' => $messages,),'at' => array ('atMobiles' => array (),'isAtAll' => false,),);
$data_string = json_encode($data);//echo $data_string;

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8'));curl_setopt($ch, CURLOPT_URL,$urls);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);curl_setopt($ch, CURLOPT_POST,true);  
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); $retBase = curl_exec($ch);

curl_close($ch);
//echo $retBase;

//根据返回值做出判断,不是成功直接抛出返回的JSON。$ret=json_decode($retBase,true);
//echo $ret."<br>";if ($ret["errmsg"]=="ok" && $ret["errcode"]=="0" )
{echo "成功";
}else{echo "失败";
}

?>

展开
收起
泡面tm 2017-04-12 16:54:08 11716 0
5 条回答
写回答
取消 提交回答
  • list($s1, $s2) = explode(' ', microtime());

    $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);

    $secret = 'xxxxx';

    $data = $timestamp . "\n" . $secret;

    $signStr = base64_encode(hash_hmac('sha256', $data, $secret));

    $signStr = utf8_encode(urlencode($signStr));

    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx";

    $webhook .= "&timestamp=$timestamp&sign=$signStr";

    这总是提示签名不匹配是怎么肥四{"errcode":310000,"errmsg":"sign not match"}

    2019-11-13 19:10:13
    赞同 1 展开评论 打赏
  • 回 2楼泡面tm的帖子
    感激不尽!终于搞定了,官方demo、真是垃啊

    不懂得朋友我附上全代码

    <?php
    function request_by_curl($remote_server, $post_string) {  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $data = curl_exec($ch);
        curl_close($ch);  
                  
        return $data;  
    }  

    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=XXX";
    $message="我就是我, 是不一样的烟火";
    $data = array ('msgtype' => 'text','text' => array ('content' => $message));
    $data_string = json_encode($data);

    $result = request_by_curl($webhook, $data_string);  
    echo $result;
    ?>
    2019-03-09 13:39:27
    赞同 展开评论 打赏
  • Re钉钉自定义机器人PHP版如何接入
    求完整的php!
    2018-04-20 09:32:13
    赞同 展开评论 打赏
  • Re钉钉自定义机器人PHP版如何接入
    谢谢楼主,折腾了我半天,总算搞好了

    -------------------------

    Re钉钉自定义机器人PHP版如何接入
    谢谢楼主,非常感谢,折腾了我半天,我都回了三次如果再不成功就不回了
    2018-04-12 14:37:39
    赞同 展开评论 打赏
  • Re钉钉自定义机器人PHP版如何接入
    用官方的样本,进行测试也是一样木有返回值,是不是环境问题
    <?php  
      
    function request_by_curl($remote_server, $post_string) {  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $data = curl_exec($ch);
        curl_close($ch);  
                  
        return $data;  
    }  

    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
    $message="我就是我, 是不一样的烟火";
    $data = array ('msgtype' => 'text','text' => array ('content' => $message));
    $data_string = json_encode($data);

    $result = request_by_curl($webhook, $data_string);  
    echo $result;

    ?>

    -------------------------

    Re钉钉自定义机器人PHP版如何接入
    找到问题了,因为提交的是https协议,所以需要加上 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
    官方的demo也是这样的情况
    2017-04-12 16:58:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
PHP安全开发:从白帽角度做安全 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载