2017-05-30 76 views
1

我有一個CURL代碼,我用它來與GetResponse集成,我認爲不好,繼續複製/粘貼它。出於某種原因,根本沒有任何錯誤,但鬆散的請求是空的(Postman使用Postman工作就可以了)。我錯過了什麼?我整晚都找不到解決方案。如何正確發送JSON CURL到Slack?

<?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; 
?> 

我總是得到一個確定。

回答

1

既然你din't,這樣你就得到什麼,從函數返回任何內部$slackReporting_res。做象下面這樣: -

<?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 
?> 

現在運的遇到錯誤,並通過這個環節來解決(在評論中提到OP) : -

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

+0

的代碼示例我的例子是'NULL'。 – Ricardo

+0

'警告:curl_errno():2不是在線16上的有效cURL句柄資源現在我很困惑。 – Ricardo

+0

太棒了!這是SSL錯誤'請求錯誤:SSL證書問題:無法獲得本地頒發者證書',我已經解決了這個帖子的幫助:https://stackoverflow.com/questions/28858351/php-ssl -certificate-error-unable-to-get-local-issuer-certificate – Ricardo

0

下面是如何使用疏鬆與curl

一個簡單的例子

摘自here