Reputation: 2232
Doing some tests with cURL as client and the CodeIgniter Rest Server. GET, POST and DELETE methods work perfectly but not PUT.
Here is my client code for PUT. It's the same as POST (except for CURLOPT_CUSTOMREQUEST):
<?php
/**
* Keys
*/
include('inc/keys.inc.php');
/**
* Data sent
*/
$content = array(
'name' => 'syl',
'email' => '[email protected]'
);
/**
* Source
*/
$source = 'http://localhost/test-rest-api-v2/api_apps/app/id/1';
/**
* Init cURL
*/
$handle = curl_init($source);
/**
* Headers
*/
$headers = array(
'X-API-Key: '. $public_key
);
/**
* Options
*/
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
/**
* For POST
*/
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $content);
/**
* Result
*/
$result = curl_exec($handle);
/**
* Close handle
*/
curl_close($handle);
echo $result;
?>
I also tried to add to the headers: 'Content-Type: application/x-www-form-urlencoded',
. Same result.
My server code:
<?php
function app_put() {
var_dump($this->put());
}
?>
Result:
array(1) { ["------------------------------1f1e080c85df Content-Disposition:_form-data;_name"]=> string(174) ""name" syl ------------------------------1f1e080c85df Content-Disposition: form-data; name="email" [email protected] ------------------------------1f1e080c85df-- " }
What's wrong with the PUT method?
Upvotes: 0
Views: 1328
Reputation: 31
I had the same problem and found this post. Then I found http_build_query and that did the trick without "simulate" a file upload.
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($content));
Upvotes: 2
Reputation: 2232
Just found the right way. You have to "simulate" a file uploading. For PUT requests only:
<?php
/**
* Data
*/
$data = array(
'name' => 'syl',
'email' => '[email protected]'
);
/**
* Convert array to an URL-encoded query string
*/
$data = http_build_query($data, '', '&');
/**
* Open PHP memory
*/
$memory = fopen('php://memory', 'rw');
fwrite($memory, $data);
rewind($memory);
/**
* Simulate file uploading
*/
curl_setopt($handle, CURLOPT_INFILE, $memory);
curl_setopt($handle, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($handle, CURLOPT_PUT, true);
?>
Upvotes: 0