[英]Posting JSON using PHP Curl
我試圖在我的本地服務器上發布PHP中的一些JSON數據。 我在下面有以下代碼,但它不起作用。 我錯過了一件至關重要的事嗎?
$url = 'http://localhost/mmcv/chart1.php';
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));
即使您發布json,您仍然必須以url編碼形式發送數據,因此請發送它
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/x-www-form-urlencoded'));
並使用
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
代替
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
也許你的問題在接收腳本中。 在“mmcv / chart1.php”中嘗試以下操作:
$rawInput = file_get_contents('php://input');
if(is_string($rawInput)) {
if(!is_null($jsonInput = json_decode($rawInput, true)))
$_POST = $jsonInput;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.