PHP 发送带有附件和变量值的松弛消息
PHP send slack messages with attachments and variable values
正在尝试 send messages to slack
已安装 incoming webhooks
的频道。 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}
的值。但是,它似乎不起作用,程序无法将 post 消息发送到松弛通道。同样,我想在 attachments -> fields
值中打印 url1
和 url2
的值,如上所示(我尝试打印的方式)。如果我在 posting 消息时不尝试使用任何变量并获取它们的值,该程序就可以正常工作。如何在消息中打印这些变量的值?
(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
]
)
]);
正在尝试 send messages to slack
已安装 incoming webhooks
的频道。 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}
的值。但是,它似乎不起作用,程序无法将 post 消息发送到松弛通道。同样,我想在 attachments -> fields
值中打印 url1
和 url2
的值,如上所示(我尝试打印的方式)。如果我在 posting 消息时不尝试使用任何变量并获取它们的值,该程序就可以正常工作。如何在消息中打印这些变量的值?
(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
]
)
]);