我有一個JSON發送到一個REST API此捲曲功能:通過PHP REST API檢索JSON
$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();
當我執行卷曲命令它返回我這個:
Warning: json_decode() expects parameter 1 to be string, array given in
那是怎麼回事?
謝謝。
混了'和'json_decode' json_encode'? – Brian
可能重複的[如何在PHP中獲取POST的主體?](http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php) – Quentin
因爲' $ _REQUEST'通常是一個數組,即使是空的。你的JSON blob不會出現在那裏,但是'php:// input'。 – mario