将数组传递给 API

Passing array to an API

我正在尝试将数组传递给 API,当我在 POSTMAN (raw form)

中运行时,API 采用以下格式的数组
{
"records": [
    {
    "content": "50.150.50.55",
    "type": "A",
    "name": "test.mauqe.com",
    "prio": null,
    "ttl": 3600
    }
    ]
}

当我尝试以这种格式在我的代码中传递数组时,

 $data = array(
             "content" => "50.150.50.55",
             "type" => "A",
             "name" => "gulpanra.mauqe.com",
             "prio" => "null",
             "ttl" => "3600"
             );

我不明白,有什么问题。响应说错误(Data sending format error)。请帮忙

使用 json_encode 将数组转换为 json 格式,然后将其传递给 api。

您正在使用的 Api 需要 json 格式的数据。

$data = json_encode($data);

您需要将数组转换为 json 格式,以便将其传递给 api。使用 json_encode()。使用下面的代码

$array = array( "content" => "50.150.50.55", "type" => "A", "name" => "gulpanra.mauqe.com", "prio" => "null", "ttl" => "3600" );
$data = json_encode($data); // Pass this to API

希望对您有所帮助

API 需要一组地图。以下是地图数组。

[
    {
    "content": "50.150.50.55",
    "type": "A",
    "name": "test.mauqe.com",
    "prio": null,
    "ttl": 3600
    },
    {},
    {},
    ...
]

你传递的是不一样的。您正在传递一张地图

{
         "content" => "50.150.50.55",
         "type" => "A",
         "name" => "gulpanra.mauqe.com",
         "prio" => "null",
         "ttl" => "3600"
 }

尝试将 $data 修改为:

$data = array();

array_push($data['records'], array(
         "content" => "50.150.50.55",
         "type" => "A",
         "name" => "gulpanra.mauqe.com",
         "prio" => "null",
         "ttl" => "3600"
));

你必须在外面制作数组,例如

您正在使用这种类型的数组进行编码

$data['records'] = array(
       'content' => '50.150.50.55',
       and so on
);

将此数组更改为此

$data = array(
  'content' => '50.150.50.55',
   and so on
);

这会有所帮助

<?php
$data = array('records' => array());
$data['records'][] = array( 

                "content" => "50.150.50.55",
                "type" => "A",
                "name" => "gulpanra.mauqe.com",
                "prio" => null,
                "ttl" => 3600

        );

$json_output = json_encode( $data );
echo $json_output;
?>

这将给出以下输出:

{"records":[{"content":"50.150.50.55","type":"A","name":"gulpanra.mauqe.com","prio":null,"ttl":3600}]}