PHP POST'ing JSON 编码数组数据
PHP POST'ing JSON encoded array data
我正在尝试将 POST JSON 编码数据发送到驻留在我们公司 Microsoft Teams 频道之一中的 Webhook 端点。它接受基本的 JSON 编码有效载荷。
我在本地计算机上的 PostMan 中开始,POST 将以下内容添加到我的 Teams Channel 连接器 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>" }
]
}
]
}
这很好用,它 post 将卡片放入 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 脚本能够 post 将一张卡片放入 Teams 频道,只是下面没有操作按钮。所以我的问题在于我如何对 $postdata 中的附加数组进行编码?
老实说,我对 PHP 中数组的了解有限,我认为我这样做是正确的,但显然我遇到了问题,大声笑。是否有 different/better/more 正确的方法将数组内部的多个数组编码为 JSON 数据到 POST?
potentialAction
在你原来的 JSON 中是一个对象数组,但你只在你的 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 时它仍然是一个数组;内部数组有关联键,所以它自动成为一个对象。)
编辑:如评论中所述,targets
属性 内部也是如此。编辑了代码,在那里也插入了一个额外的级别。
我正在尝试将 POST JSON 编码数据发送到驻留在我们公司 Microsoft Teams 频道之一中的 Webhook 端点。它接受基本的 JSON 编码有效载荷。
我在本地计算机上的 PostMan 中开始,POST 将以下内容添加到我的 Teams Channel 连接器 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>" }
]
}
]
}
这很好用,它 post 将卡片放入 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 脚本能够 post 将一张卡片放入 Teams 频道,只是下面没有操作按钮。所以我的问题在于我如何对 $postdata 中的附加数组进行编码?
老实说,我对 PHP 中数组的了解有限,我认为我这样做是正确的,但显然我遇到了问题,大声笑。是否有 different/better/more 正确的方法将数组内部的多个数组编码为 JSON 数据到 POST?
potentialAction
在你原来的 JSON 中是一个对象数组,但你只在你的 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 时它仍然是一个数组;内部数组有关联键,所以它自动成为一个对象。)
编辑:如评论中所述,targets
属性 内部也是如此。编辑了代码,在那里也插入了一个额外的级别。