Sco*_*dge 2 php curl slack-api
我正在尝试使用 curl 使用 php 发送 slack 消息。从在线示例中,我可以通过命令行将消息发送到 curl 没有问题。我的问题是将其转换为 php。当我通过 php 使用 curl 时,新行 \n 不会作为新行传递。它以文本形式出现。
这是我的 curl 示例,该示例有效并将发送新行
curl -X POST -H 'Authorization: Bearer xxxxx-xxxxxx-xxxxx-xxxxx' -H 'Content-type: application/json' --data '{"username":"Robot_Savant","channel":"recieved_test","as_user":"false","text":"First Line of Text\nSecond Line of Text"}' https://slack.com/api/chat.postMessage
Run Code Online (Sandbox Code Playgroud)
卷曲输出:
第一行文本
第二行文本
这是我的 php 代码。
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
$data = http_build_query([
"channel" => '#recieved_test', //"#mychannel",
"text" => 'First Line of Text\nSecond Line of Text', //"Hello, Foo-Bar channel message.",
"as_user" => "false",
"username" => "Robot_Savant"
]);
curl_setopt($ch, CURLOPT_URL, "https://slack.com/api/chat.postMessage");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer xxxxx-xxxxxx-xxxxx-xxxxx";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
Run Code Online (Sandbox Code Playgroud)
通过 PHP 输出卷曲:
第一行文本\n第二行文本
我不知道如何让它发挥作用。任何帮助都会很棒。我尝试了很多不同的标题设置。我从 stackoverflow 或 slack api 示例中提取的两段代码。
改变:
"text" => 'First Line of Text\nSecond Line of Text',
Run Code Online (Sandbox Code Playgroud)
到:
"text" => "First Line of Text\nSecond Line of Text",
Run Code Online (Sandbox Code Playgroud)
在 PHP 中使用单引号会将 \n 输出为 \n,而不是作为换行符。
| 归档时间: |
|
| 查看次数: |
1727 次 |
| 最近记录: |