1
我有以下數據通過我的API發佈到服務器。使用PHP和cURL將數據發送到服務器
Request Body:
{
"nutrients": {
"protein": "beans",
"fats": "oil",
"carbohdrate": "starch"
}
}
每次我運行該腳本,我得到以下
{"errors":[{"status":"400","code":"031","title":"payload not parseable","detail":"Invalid formatting of the request payload."},{"status":"400","code":"026","title":"payload missing","detail":"No payload describing the resource object included in the request."}]}
以下錯誤看來,這是因爲我不是添加數據數組中的變量「營養素」被張貼。
有人可以幫我一把。以下是我迄今的努力。謝謝
<?php
$url = "https://myapi_site.com/server/";
$data = array(
'protein' => 'beans',
'fats' => 'oil',
'carbohdrate' => 'starch'
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
?>
錯誤仍然存在,似乎是因爲我沒有在要發佈的數據數組中添加變量「營養素」。是否有可能做類似$ data = array(營養素( 'protein'=>'beans', 'fats'=>'oil', 'carbohdrate'=>'starch' )); – nackolysis
請我們添加營養素,因爲我看不到任何營養更新代碼 – nackolysis
我編輯我的答案有一個$數據更新變量,複製並粘貼到您的代碼。是的,只要使用這個'$ data = array(營養素('蛋白質'=>'豆','脂肪'=>'油','碳水化合物'=>'澱粉'));' – Clay