【问题标题】:PHP POST'ing JSON encoded array dataPHP POST'ing JSON 编码的数组数据
【发布时间】:2020-02-21 10:28:38
【问题描述】:

我正在尝试将 JSON 编码数据发布到驻留在我们公司 Microsoft Teams 频道之一中的 webhook 端点。它接受基本的 JSON 编码负载。

我从本地计算机上的 PostMan 开始,并将以下内容发布到我的团队频道连接器 webhook URL:

{
 "@context": "https://schema.org/extensions"
 "@type": "MessageCard",
 "themeColor": "0072C6",
 "title": "Test From PostMan",
 "text": "This is the text body of the notification card",
 "potentialAction": [
    {
      "@type": "OpenUri",
      "name": "View in Browser...",
      "targets": [
         { "os": "default", "uri": "https://<REDACTED>" }
      ]
    }
  ]
}

这很好用,它会将卡片发布到 Teams 频道,并在其下方显示操作按钮。

所以我转向 PHP,并编写了以下代码:

<?php
//api endpoint
$url = 'https://<REDACTED>';

//new curl connection
$ch = curl_init($url);

//build json data array
$postdata = array(
  '@context' => 'https://schema.org/extensions',
  '@type' => 'MessageCard',
  'themeColor' => '0072C6',
  'title' => 'Test from curl in PHP',
  'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..',
    'potentialAction' => array (
       '@type' => 'OpenUri',
       'name' => 'View in Browser...',
       'targets' => array (
          'os' => 'default',
          'uri' => 'https://<REDACTED>'
       )
   )
);

//encode json data array
$encodeddata = json_encode($postdata);

//set curl options
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodeddata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

//debug
echo $result;

//close
curl_close($ch);
?>

当我运行上述程序时,API 出错并响应说它是无效的有效负载。所以我剥离了我的代码,这样我的$postdata 数组就简单多了,如下所示:

//build json data array
$postdata = array(
  '@context' => 'https://schema.org/extensions',
  '@type' => 'MessageCard',
  'themeColor' => '0072C6',
  'title' => 'Test from curl in PHP',
  'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..'
);

这很好用,我的 PHP 脚本能够将卡片发布到 Teams 频道,只是它下面没有操作按钮。所以我的问题在于我如何在 $postdata 中编码额外的数组?

老实说,我对 PHP 数组的了解有限,我认为我这样做是正确的,但显然我遇到了问题,哈哈。是否有不同/更好/更正确的方法将数组内的多个数组编码为 JSON 数据以进行 POST?

【问题讨论】:

  • 我建议您尝试发布到仅转储 $_POST 的本地 PHP 脚本,然后在 JSON 验证器(即jsonlint.com)上验证整个发布 JSON 有效负载。

标签: php json api post encode


【解决方案1】:

您原始 JSON 中的potentialAction 是一个对象数组,但您仅在 PHP 数据结构中将其设为单级数组。

您需要将其包装到一个额外的级别:

$postdata = array(
  '@context' => 'https://schema.org/extensions',
  '@type' => 'MessageCard',
  'themeColor' => '0072C6',
  'title' => 'Test from curl in PHP',
  'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..',
    'potentialAction' => array (
       array (
         '@type' => 'OpenUri',
         'name' => 'View in Browser...',
         'targets' => array (
            array (
              'os' => 'default',
              'uri' => 'https://<REDACTED>'
            )
         )
       )
   )
);

当您将其编码为 JSON 时,这将为您提供该位置的对象数组。 (外部数组有一个从零开始的数字索引,所以在转换为 JSON 时它仍然是一个数组;内部数组有关联键,所以它会自动成为一个对象。)


编辑:如 cmets 中所述,内部的 targets 属性也是如此。编辑了代码,以便在那里插入一个额外的级别。

【讨论】:

  • 属性targets似乎仍然存在同样的问题
  • @CBroe 谢谢你。你说得对。正如 Fanax 所说,我的 targets 属性也存在同样的问题。对潜在操作和目标都应用了修复,API 调用成功。
  • @Fanax 谢谢,忽略了我自己,我相应地编辑了代码。
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多