PHP发送带有附件和变量值的松弛消息

时间:2016-08-18 20:33:28

标签: php string variables slack-api slack

尝试安装send messages to slack的{​​{1}}个频道。 incoming webhooks需要与消息一起发送,PHP变量包含这些URL。同样,我想发送一些Attachments,它们在一些PHP变量中再次保存。这是我的ID's代码:

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}值中打印url1url2的值,如上所示(我正在尝试打印的方式)。如果我不尝试使用任何变量并在发布消息时获取其值,该程序就可以正常工作。如何在消息中打印这些变量的值?

attachments -> fields

1 个答案:

答案 0 :(得分:1)

请尝试使用&gt;

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