Search code examples
phpcurlyiiwebhooksslack

Cannot post payload to slack via yii app


I am looking to post a simple Slack incoming WebHook from my yii app but I can't seem to get anything delivered to Slack. I believe I've enabled curl in my php.ini, I've been using curl wrapper from the yii extensions (https://github.com/hackerone/curl)

Here's my code;

$url = Options::getSlackWebhookUrl();

        $json_data = json_encode(array(
            "text"=>"I'm a notification from yiiapp to Slack channel <" . Options::getSlackChannel() . ">", 
            "username"=> "yiiapptest",
            "channel"=> Options::getSlackChannel()
        ));
        $post_data['json_data'] = $json_data;

        $output = Yii::app()->curl->post($url, $post_data);

        // Just for debug: to see response
        echo "<pre>" . $url . " + " . $output . " (" . $json_data . ")</pre>";

Here's my output to view;

https://hooks.slack.com/services/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 + No payload received ({"text":"I'm a notification from yiiapp to Slack channel <#digital>","username":"yiiapptest","channel":"#digital"})

My $output just gives me a "No payload received". If I use urlenode() on my url, I get an 'Error 500 - Undefined Index:scheme'

Can anyone see where I'm going wrong?


Solution

  • you need to change

        $post_data['json_data'] = $json_data;
    

    to

        $post_data['payload'] = $json_data;
    

    it’s awaiting post variable with name ‘payload’