How to perform a cURL PUT request in PHP?

To perform a cURL PUT request in PHP, you can use the following code:

<?php $url = '<url>'; // Replace with the URL you want to send the PUT request to $data = array( 'key1' => 'value1', 'key2' => 'value2' ); // Replace with your desired data to be sent $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); // Print the response echo $response; ?>

Make sure to replace <url> with the actual URL you want to send the PUT request to. Also, replace the $data array with your desired data to be sent as a JSON payload.

This code sets the request method to PUT using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT') and the data to be sent using curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)). Finally, the response is captured using curl_exec($ch) and printed using echo $response.