Posting JSON to PHP with cURL
In web development, communicating data between client and server in JSON format is a common practice. However, when attempting to post JSON data to a PHP script using the cURL command, it may fail to interpret the data correctly, resulting in an empty array.
This issue commonly arises because the -d parameter in cURL is typically interpreted as form-encoded data. To specify that the data is in JSON format, the -H parameter must be used to set the Content-Type header.
Corrected Command
To solve the issue, modify the command as follows:
curl -v -H "Content-Type: application/json" -X POST -d '{"screencast":{"subject":"tools"}}' \ http://localhost:3570/index.php/trainingServer/screencast.json
Explanation
By using this corrected command, PHP should now correctly interpret the POST data and act accordingly.
The above is the detailed content of How to Correctly Post JSON Data to a PHP Script Using cURL?. For more information, please follow other related articles on the PHP Chinese website!
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}