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
- How can the conflict between the object tag and the content/header output be resolved when displaying .swf files in PHP?
- What are some best practices for closing statements and database connections in PHP?
- How can Excel calculations be integrated into a website, allowing users to input values and receive results?