1

after I succeeded in making a POST request and grab the values in the web service i'm building. I'm facing a problem regarding the Put Request. I managed to make the Put Request and I sent an array containing name and id for the update purpose this way:

curl_setopt($ic, CURLOPT_POSTFIELDS, http_build_query($data));

But when I try to grab the id sent using $_POST['id'] I get the undefined index error, I printed_r($_POST) and it's empty. Now I dont't believe there is a super global array for PUT like for POST and even if it exists , I don't think there is :

curl_setopt($ic, CURLOPT_PUTFIELDS, http_build_query($data));

in curl have you even been through a similar error? any idea?

To take a look at my previous post concerning the post request to have a better understanding of what I'm trying to do , it's here

Community
  • 1
  • 1
arakibi
  • 441
  • 7
  • 24

4 Answers4

4

Try this

curl_setopt($ic, CURLOPT_PUTFIELDS, json_encode($data));

and take it by

$array_get = json_decode(file_get_contents('php://input'));
tesst
  • 149
  • 1
  • 11
3

use this CURLOPT_CUSTOMREQUEST =PUT and then just set values with CURLOPT_POSTFIELDS

or you can use custom header CURLOPT_HTTPHEADER eg.

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));

The script below demonstrates how you make a PUT request.

$ch = curl_init();    

curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // note the PUT here

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string)                                                                       
));       

// execute the request

$output = curl_exec($ch);

// close curl resource to free up system resources

curl_close($ch);
munsifali
  • 1,732
  • 2
  • 24
  • 43
  • hey thanks for your help , I'm not having problem with making the Put request rather with how to get values passed in the other side ... – arakibi Dec 18 '14 at 11:55
2

The $_POST only for method=post;

You use method=put, so the $_POST is empty.

You can get the putdata like this:

$_PUT = array();
if('PUT' == $_SERVER['REQUEST_METHOD']){
    parse_str(file_get_contents('php://input'), $_PUT);
}
Ateoa
  • 116
  • 3
0

This is the secret to sending a PUT request:

curl_setopt($ch, CURLOPT_POST, true);  // <-- NOTE this is POST
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // <-- NOTE this is PUT

A full example:

$vals = array("email" => "hi@example.com", "phone" => "12345");
$jsonData = json_encode($vals);

curl_setopt($ch, CURLOPT_POST, true);  // <-- NOTE this
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // <-- NOTE this

//We want the result / output returned.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/json',
    'Content-Length:' . strlen($jsonData),
    'X-Apikey:Any_other_header_value_goes_here'
));

//Our fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

//Execute the request.
$response = curl_exec($ch);

echo $response;
zundi
  • 2,361
  • 1
  • 28
  • 45