У меня есть эта Curl функцию, отправить JSON в REST API:Получить JSON с помощью PHP REST API
$url = "https://server.com/api.php";
$fields = array("method" => "mymethod", "email" => "myemail");
$result = sendTrigger($url, $fields);
function sendTrigger($url, $fields){
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=UTF-8"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlResult["msg"] = curl_exec($ch);
$curlResult["http"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $curlResult;
}
На сервере, у меня есть этот код:
$data = json_decode($_REQUEST);
var_dump($data);
exit();
Когда я исполню CURL команда возвращает мне это:
Warning: json_decode() expects parameter 1 to be string, array given in
Как это сделать?
Спасибо.
перепутаны '' json_encode' с json_decode'? – Brian
Возможный дубликат [Как получить тело POST в php?] (Http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php) – Quentin
Поскольку ' $ _REQUEST' - это всегда массив, даже пустой. Ваш JSON blob не появляется там, но 'php: // input'. – mario