2016-08-18 53 views
0

嘗試send messages to slack通道與incoming webhooks安裝在它們上面。 Attachments需要與消息一起發送,PHP變量保存這些URL。同樣,我想發送一些ID's,它們又保留在一些PHP變量中。這裏是我的server side PHP代碼:PHP發送帶有附件和變量值的鬆弛消息

<?php 

$testplan_name = $_POST[plan]; //test plan name coming from the client 
$url1 = $_POST[run_url]; //run url coming from the client 
$url2 = $_POST[plan_url]; //plan url coming from the client 
$room = "random"; 
$icon_url = ":ghost:"; 
$username = "Test"; 
$attachments = array([ 
      'fallback' => 'Hey! See this message', 
      'pretext' => 'Here is the plan name ${testplan_name}', 
      'color' => '#ff6600', 
      'fields' => array(
       [ 
        'title' => 'Run URL', 
        'value' => 'url1', 
        'short' => true 
       ], 
       [ 
        'title' => 'Build URL', 
        'value' => 'url2', 
        'short' => true 
       ] 
      ) 
     ]); 

$data = "payload=" . json_encode(array(   
     "channel"  => "#{$room}", 
     "icon_emoji" => $icon_url, 
     "username"  => $username, 
     "attachments" => $attachments 

    )); 

$url = "https://hooks.slack.com/services/XXXX/XXX/XXXXXXXXXXXXX"; //got from slack as a webhook URL 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$result = curl_exec($ch); 
echo var_dump($result); 
if($result === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 

curl_close($ch); 

如果您在attachments變量見上面,有一個pretext變量中,其試圖打印的${testplan_name}頂部公佈值。然而,它似乎沒有工作,並且該程序未能將消息發佈到閒置頻道。同樣,我想要在attachments -> fields值中打印url1url2的值,如上所示(我試圖打印的方式)。如果我不嘗試使用任何變量並在發佈消息時獲取它們的值,該程序就可以正常工作。如何在消息中打印這些變量的值?

(slack is a messaging platform for teams, if you don't know)

回答

0

試試這個>

$attachments = array([ 
     'fallback' => 'Hey! See this message', 
     'pretext' => 'Here is the plan name '.$testplan_name, 
     'color' => '#ff6600', 
     'fields' => array(
      [ 
       'title' => 'Run URL', 
       'value' => $url1, 
       'short' => true 
      ], 
      [ 
       'title' => 'Build URL', 
       'value' => $url2, 
       'short' => true 
      ] 
     ) 
    ]);