我的后端(Node JS)希望获得以下信息,以便在我的数据库(MongoDB)中创建一个文档
{
"Name": "John Doe",
"items": [
{
"name": "Item A",
"price": 100,
},
{
"name": "Item B",
"price": 250,
}
]
}
我正在尝试使用这种结构在PHP(WordPress)中创建一个请求,并将其发送到我的服务器。这是我当前拥有的代码:
$body = (object)array('Name' => 'John Doe', 'items' => array(
array("name" => "Item A", "price" => 5000),
array("name" => "Item B", "price" => 5000),
));
但是这行不通,我很感谢您的帮助,因为我十年来没有写过php
好吧,我似乎缺少的是对请求的一些配置。
对发现有用的人的答复:
$url = "http://example.com";
$body = array('name' => 'John Doe', 'items' => array(
array("name" => "Item A", "price" => 5000),
array("name" => "Item B", "price" => 5000),
));
$body = json_encode($body);
print_r($body);
$response = wp_remote_post($url, array(
'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
'body' => $body,
'method' => 'POST',
'data_format' => 'body'
));