PHP发送带有附件和变量值的空闲消息


PHP send slack messages with attachments and variable values

尝试安装incoming webhookssend messages to slack通道。Attachments需要与消息和PHP变量一起发送这些url。类似地,我想发送一些ID's,它们再次保存在一些PHP变量中。以下是我的server side 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)

试试这个>

    $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
            ]
        )
    ]);