负载无效 JSON Slack Webhooks PHP

Payload was not valid JSON Slack Webhooks PHP

Slack 传入 Webhooks returns 有效负载无效 JSON 文档似乎没有多大意义。

public function index() {

    $message = [
        "pretext"    => "There's been a sale on Example Cart",
        "title"      => "Sales Order #123456 on Example Cart",
        "title_link" => "http://www.example.com/admin/index.php?route=sale/order/info&order_id=123456",
        "text"       => "There's been a new sale on the Example website. Click the order above to check it out.",
    ];

    $send = self::slack($message);

    dd($send); // die and dump curl result

}

public static function slack($message, $room = "test-messages", $color = "#ED7100", $icon = ":mailbox:") {

    foreach ($message as $key => $value):
        $message[$key] = str_replace(["<", ">", "&"], ["&lt;", "&gt;", "&amp;"], $value);
    endforeach;

    $fallback = $message["pretext"] . " - " . $message["title"] . " - " . $message["title_link"];

    $attachments = [
        "fallback"   => $fallback,
        "pretext"    => $message["pretext"],
        "title"      => $message["title"],
        "title_link" => $message["title_link"],
        "text"       => $message["text"],
        "color"      => $color,
    ];

    $payload = [
        "channel"     => "#{$room}",
        "channel"     => "@bossman",
        "icon_emoji"  => $icon,
        "username"    => "webhook-tests",
        "attachments" => [$attachments]
    ];

    $data = "payload=" . json_encode($payload);

    $ch = curl_init("https://hooks.slack.com/services/endpoint");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

当我在任何在线 linters 中检查 JSON 时,它 returns 有效 JSON。

Slack 正在寻找文档中没有的内容?

您的 POST 数据似乎在发送前 payload= 而不是密钥。

This gist 像这样发送数据:

$data = json_encode($payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => $data));