1

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.

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Ricardo
  • 1,623
  • 7
  • 23
  • 49

2 Answers2

1

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

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
  • With your example I get `NULL`. – Ricardo May 30 '17 at 09:01
  • `Warning: curl_errno(): 2 is not a valid cURL handle resource in on line 16` I'm confused now. – Ricardo May 30 '17 at 09:31
  • Awesome! It was an error with the SSL `Request Error:SSL certificate problem: unable to get local issuer certificate` and I've fixed it with the help of this post: https://stackoverflow.com/questions/28858351/php-ssl-certificate-error-unable-to-get-local-issuer-certificate – Ricardo May 30 '17 at 09:39
  • @Ricardo glad to help you:):) – Anant Kumar Singh May 30 '17 at 09:40
0

Here is a simple example of how to use slack with curl

<?php
define('SLACK_WEBHOOK', 'https://hooks.slack.com/services/xxx/yyy/zzz');
function slack($txt) {
  $msg = array('text' => $txt);
  $c = curl_init(SLACK_WEBHOOK);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_POST, true);
  curl_setopt($c, CURLOPT_POSTFIELDS, array('payload' => json_encode($msg)));
  curl_exec($c);
  curl_close($c);
}
?>

Snippet taken from here

Uri Goren
  • 12,532
  • 6
  • 50
  • 100