2016-08-18 99 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 
      ] 
     ) 
    ]);