How To Use Curl Put Request With Php Example

03-Apr-2023

.

Admin

Hi Guys,

In this example,I will learn you how to use curl put request with php.you can easy and simply use curl put request with php.

you'll learn php curl put request with body. i explained simply step by step how to call put request with data using php curl.

PHP Curl PUT Request:


<?php

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => "https://api.nicesnippets.com/api/users/7",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "PUT",

curl_setopt($ch, CURLOPT_POSTFIELDS, [

'first_name' => 'Demo',

'last_name' => 'Test',

'email' => 'demo@gmail.com'

]),

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;

}

?>

It will help you..

#PHP