有效负载不是有效的JSON Slack Webhooks PHP


Payload was not valid JSON Slack Webhooks PHP

Slack Incoming Webhooks返回Payload不是有效的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;
}

当我在任何在线linter中对JSON进行lint时,它将作为有效的JSON返回。

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

看起来您的POST数据是用payload=而不是密钥发送的。

这个要点发送这样的数据:

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