我一直试图弄清楚如何在没有任何运气的情况下对这种json提出发布请求
URL:/v1.0/country/{countryId}/city/search
Method:POST
Content-Type: application/json
Body: {\"city\":[{\"cityId\":1234, \"min\":0, \"max\":15}]}
有什么想法吗?
编辑: 这是我正在使用的代码
<?php
// Auth
$auth = base64_encode("userauthurl:password");
$authContext = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$tokenUrl = 'https://tokenurl.com/token';
$tokenResponse = file_get_contents($tokenUrl, false, $authContext );
$tokenObj = json_decode($tokenResponse);
$access_token = $tokenObj->access_token;
// POST params
$postData = array(
'body' => '{\"city\":[{\"cityId\":1234, \"min\":0, \"max\":5}]}'
);
// Context POST
$contextGet = stream_context_create([
"http" =>
["method" => "POST",
"header" => "Accept: application/json\r\n".
"Authorization: Bearer $access_token\r\n",
"content" => json_encode($postData)
]]);
$cityUrl = 'https://urltosendto.com/v1.0/country/{countryId}/city/search';
$cityResponse = file_get_contents($cityUrl, false, $contextGet);
$city = json_decode($cityResponse);
echo json_encode($cityResponse);
?>