Slack中的消息格式使用块布局[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我想使用块布局格式向我的松弛app发送消息。我在PHP中创建一个关联数组,然后使用json_encode()将其转换为JSON。问题是它没有转换为slack期望的JSON格式,我得到一个错误'无效块格式'。这是我的代码,输出和松弛所期望的输出。

$data = array(
    'blocks' => array(
        'type' => 'mrkdwn',
        'text' => 'Danny Torrence left the following review for your property'
    ),
);
$data = json_encode($data);

我得到以下输出:

{"blocks":{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}}

但是,Slack期望以下格式的JSON:

{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}

我只需要在最后将一个'{'转换为'['和一个'}'转换为']'。我将不胜感激任何帮助。

谢谢

php json slack-api
1个回答
2
投票

我没有足够的声誉,但我相信这是一个重复:no square bracket json array

此外,这是无效的json,你可以检查https://jsonlint.com/?code=

{"blocks":["type":"mrkdwn","text":"Danny Torrence left the following review for your property"]}

总结一下这篇文章,你真正需要做的就是用另一个数组包装你的内部数组

$data = array(
    'blocks' => array(array(
        'type' => 'mrkdwn',
        'text' => 'Danny Torrence left the following review for your property'
    )),
);

这会返回:

{"blocks":[{"type":"mrkdwn","text":"Danny Torrence left the following review for your property"}]}
© www.soinside.com 2019 - 2024. All rights reserved.