Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL HTTP PUT

I am trying to create a HTTP PUT request with cURL and I can't make it work. I've read many tutorials but none of them actually worked. Here's my current code:

$filedata = array('metadata' => $rdfxml); $ch = curl_init($url); $header = "Content-Type: multipart/form-data; boundary='123456f'"; curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata)); $returned = curl_exec($ch);  if (curl_error($ch)) {     print curl_error($ch); } else {     print 'ret: ' .$returned; } 

I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!

like image 410
user601513 Avatar asked Feb 18 '11 15:02

user601513


People also ask

How to do PUT request on Curl in php?

To make a PUT request with Curl, you need to use the -X PUT command-line option. PUT request data is passed with the -d parameter. If you give -d and omit -X, Curl will automatically choose the HTTP POST method. The -X PUT option explicitly tells Curl to select the HTTP PUT method instead of POST.

What is Curlopt_returntransfer in Curl?

CURLOPT_RETURNTRANSFER: Converts output to a string rather than directly to the screen. CURLOPT_HTTPHEADER: Request header information with an array of parameters, as shown in the example of "Browser-based redirection"

What is Curl_setopt?

The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option.


2 Answers

Just been doing that myself today... here is code I have working for me...

$data = array("a" => $a); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));  $response = curl_exec($ch);  if (!$response)  {     return false; } 

src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

like image 144
Brian Avatar answered Sep 23 '22 08:09

Brian


Using Postman for Chrome, selecting CODE you get this... And works

<?php    $curl = curl_init();    curl_setopt_array($curl, array(    CURLOPT_URL => "https://blablabla.com/comorl",    CURLOPT_RETURNTRANSFER => true,    CURLOPT_ENCODING => "",    CURLOPT_MAXREDIRS => 10,    CURLOPT_TIMEOUT => 30,    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,    CURLOPT_CUSTOMREQUEST => "PUT",    CURLOPT_POSTFIELDS => "{\n  \"customer\" : \"con\",\n  \"customerID\" : \"5108\",\n  \"customerEmail\" : \"[email protected]\",\n  \"Phone\" : \"34600000000\",\n  \"Active\" : false,\n  \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",    CURLOPT_HTTPHEADER => array(      "cache-control: no-cache",      "content-type: application/json",      "x-api-key: whateveriyouneedinyourheader"    ),  ));    $response = curl_exec($curl);  $err = curl_error($curl);    curl_close($curl);    if ($err) {    echo "cURL Error #:" . $err;  } else {    echo $response;  }    ?>
like image 35
Jordi Serra Avatar answered Sep 26 '22 08:09

Jordi Serra