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);
}
If you look at the documentation, It says that when you set CURLOPT_PUT
to true
then 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With