I have a curl request as below, which should send a PUT request to a PHP file on my webserver. When I receive this request, I want to list all of the POST data received with this PUT request, but no POST data is received. Please can you tell me where I am going wrong?
$ch = curl_init();
$fields = array(
'username' => 'username'
);
curl_setopt($ch, CURLOPT_URL, "http://localhost/linetime/user/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
var_dump($result);
The data sent with PUT
requests is not available through $_POST
.
Take a look at what $_SERVER['CONTENT_TYPE']
tells you. It's multipart/form-data
usually with a boundary like in MIME mails. You can read the PUT
data using the input stream:
$putData = '';
$fp = fopen('php://input', 'r');
while (!feof($fp)) {
$s = fread($fp, 64);
$putData .= $s;
}
fclose($fp);
In your case, $putData
should now contain something like:
------------------------------e85bfe7e43b5
Content-Disposition: form-data; name="username"
username
------------------------------e85bfe7e43b5--
Now, all you'll need is to parse this data using one of the many freely available PEAR packages for MIME handling.