dongnao2048 2017-05-30 08:37
浏览 88
已采纳

如何正确发送JSON CURL到Slack?

I have a CURL code that I use to integrate with GetResponse and I thought ill go ahead and copy/paste it for slack too. For some reason there are no errors at all yet slack is empty of requests (a POST to this URL with Postman works just fine). What am I missing? I couldn't find a solution the whole night.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

function slackReporting($data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://hooks.slack.com/services/XXXX/XXXX/XXXXXX');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_exec($ch);
}

$slackReporting_data = array(
    'text' => "`New Lead` `+34 today`.",
    'username' => "Leads",
    'mrkdwn' => true
);

$slackReporting_res = json_decode(slackReporting($slackReporting_data));

$slackReporting_error = "";
if(empty($slackReporting_res->error)){
    echo "OK";
} else {
    $slackReporting_error = $slackReporting_res->error->message;
}
echo $slackReporting_error;
?>

I always get an OK.

  • 写回答

2条回答 默认 最新

  • douwen1549 2017-05-30 08:44
    关注

    Since you din't return anything from function so you are getting nothing inside $slackReporting_res .Do like below:-

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    function slackReporting($data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://hooks.slack.com/services/XXXX/XXXX/XXXXXX');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $content  = curl_exec($ch);
        if(curl_errno($ch)){
           echo 'Request Error:' . curl_error($ch);exit;
        }
        curl_close($ch);
        return $content;
    }
    $slackReporting_data = array(
        'text' => "`New Lead` `+34 today`.",
        'username' => "Leads",
        'mrkdwn' => true
    );
    $slackReporting_res = json_decode(slackReporting($slackReporting_data));
    
    var_dump ($slackReporting_res); //check output and work accordingly
    ?>
    

    And now Op's got error and solved through this link(mentioned by OP in comment):-

    PHP - SSL certificate error: unable to get local issuer certificate

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波