PHP cURL API Requests: GET, POST, PUT, and DELETE
Learn how to call APIs from PHP using cURL and file_get_contents(), with practical GET, POST, PUT, and DELETE request examples you can reuse in your own projects.


To make an API request with PHP, you can use file_get_contents() or cURL.
file_get_contents() function for making a request:
<?php
$url = 'https://example.com/api';
$data = file_get_contents($url);
print_r(json_decode($data));
?>
cURL for making a request:
<?php
$url = 'https://example.com/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r(json_decode($data));
?>
In these examples, an API request is made to https://example.com/api, and the returned data is converted into a PHP array with the json_decode() function. To use the returned data, you need to iterate over the array returned by the json_decode() function.
In addition, when making API requests, you can also use HTTP request methods such as GET, POST, PUT, and DELETE. For example, POST to create a record, PUT to update a record, and DELETE to delete a record. To use these request methods, you need to use cURL.
<?php
$url = 'https://example.com/api';
$data = array("name" => "John Doe", "age" => "35");
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>
In this code example, a POST request is made, and the request information is sent with Content-type: application/x-www-form-urlencoded and http_build_query($data).
When making API requests, you generally need to use the parameters and headers specified in the API documentation. Therefore, it is recommended that you review the API documentation carefully and contact the API provider when necessary.
GET, PUT, POST, and DELETE Examples While Using an API
The most commonly used HTTP request types are GET, POST, PUT, and DELETE.
- GET: This is the request type used to retrieve data from the server. For example, it’s used to get the content of a web page.
<?php
$url = 'https://example.com/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$data = curl_exec($ch);
curl_close($ch);
print_r(json_decode($data));
?>
- POST: This is the request type used to send data to the server. For example, it’s used to create a record.
<?php $url = 'https://example.com/api'; $data = array("name" => "John Doe", "age" => "35"); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { /* Handle error */ } var_dump($result); ?> - PUT: This is the request type used to update a record on the server. For example, it’s used to change a record’s information.
<?php $url = 'https://example.com/api'; $data = array("name" => "Jane Smith", "age" => "40"); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'PUT', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { /* Handle error */ } var_dump($result); ?> - DELETE: This is the request type used to delete a record on the server. For example, it’s used to remove a record from the system.
<?php $url = 'https://example.com/api/123'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); print_r($response); ?>In this code example, a DELETE request is made to delete the record at
https://example.com/api/123. After the request is made, the response returned from the server is retrieved withcurl_exec()and printed to the screen withprint_r().However, keep in mind that a DELETE operation cannot be undone, so you should be careful before using it. That’s why I didn’t use a real API address in the example.
Also, when making API queries, you generally need to use the parameters and headers specified in the API documentation. Therefore, it’s recommended that you carefully review the API documentation and, when necessary, contact the API provider.
Share it if it helped


