Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURLOPT_PUT vs CURLOPT_POSTFIELDS

Tags:

put

php

curl

api

When I sent the PUT request API via curl to the REST, I found strange behavior. If you set the parameter curl_setopt($curl, CURLOPT_PUT, true), then queries, in which CURLOPT_POSTFIELDS is not empty, then the query execution lasts 1.5 minutes (as if it depends on some timeout). And if the same request is sent with the parameter curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"), then the query execution lasts about 1 second, as it should be. Can someone explain the fundamental difference between these parameters?

Example code:

$data = http_build_query(array("enable"=> 1));

if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, BASE_URL .'users/2');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);    
    curl_setopt($curl, CURLOPT_PUT, true); // execution time 1.5 min
    //curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, "PUT"); - execution time 1 sec
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $out = json_decode(curl_exec($curl));
    curl_close($curl);      
}
like image 362
Антон Домих Avatar asked Dec 18 '22 06:12

Антон Домих


1 Answers

If you look at the documentation, It says that when you set CURLOPT_PUT to truethen the file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE (In your case you are not setting the file).

Setting CURLOPT_CUSTOMREQUEST to PUT method is not expecting the file which is the main difference between CURLOPT_CUSTOMREQUEST and CURLOPT_PUT.

like image 150
Dileep Kumar Avatar answered Jan 08 '23 13:01

Dileep Kumar