What are the common HTTP methods used in REST API for creating and updating data?
The common HTTP methods used in REST API for creating and updating data are POST and PUT/PATCH.
// Creating data using POST method
$ch = curl_init('http://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['name' => 'John Doe', 'age' => 30]));
$response = curl_exec($ch);
curl_close($ch);
// Updating data using PUT method
$ch = curl_init('http://api.example.com/data/1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['name' => 'Jane Smith', 'age' => 35]));
$response = curl_exec($ch);
curl_close($ch);
Keywords
Related Questions
- What are the advantages and disadvantages of using PHP scripts to add watermarks to images compared to using image editing software with batch processing capabilities?
- What are some potential JavaScript errors that can occur when integrating a WYSIWYG editor into a PHP form?
- What are the best practices for structuring PHP code to avoid overlapping elements like footers covering form buttons?