REST API using PHP (Basic GET, POST, PUT & DELETE)

Dzikri Robbi, S.Tp, M.Kom
6 min readMar 29, 2021

REpresantional State Transfer (REST) is an architectural standar that uses the HTTP protocol to communicate data base on Web Service. So, with REST, users can access data/resources by a global ID or URIs.

The HTTP methods commonly used in REST API are:

  • GET, usually used to read resources from the REST server
  • POST, usually used to create new resources on the REST server
  • PUT, usually used to update resources on the REST server
  • DELETE, usually used to delete resources from the REST server
  • OPTIONS, used to get supported operations on resources from the REST server

The data provided by the REST server can be in text, JSON or XML format. The most popular in use today is the JSON format.

This post’s objective is to demonstrate how to create and run REST API using PHP and execute common REST method (GET, POST, PUT & DELETE) using Postman.

1. REST API — GET Method

This method usually used to read resources from the REST server.

First thing first, we create file get_city.php and define the header like this :

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

This header will allow user to access service with GET method and get JSON data from the service.

Then, continue to set data :

$data = array(
array(
'id' => '1',
'name' => 'Bandung'
),
array(
'id' => '2',
'name' => 'Jakarta'
),
array(
'id' => '3',
'name' => 'Surabaya'
),
);

In this post, I use array data. But you can also set the data from database query.

And then, we need to create search function for data.

if(!empty($_GET['search'])) {
$key = array_search($_GET['search'], array_column($data, 'name'),true);
$id = $data[$key]['id'];
$name = $data[$key]['name'];
$result = array(
'id' => $id,
'name' => $name,
'status' => 'success'
);
} else {
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'][] = 'success';
}

In this post, I use “search” for key. So, we can search City on data by this key.

Finally, we store the result as JSON data.

http_response_code(200);
echo json_encode($result);

Now, we can test this service with Postman. For example, if we want to get city with name “Bandung”, we can use key “search” with value “Bandung”. So, you can get the result like this :

But, if we don’t input the key, result will be show all data in JSON.

{
"city": [
{
"id": "1",
"name": "Bandung"
},
{
"id": "2",
"name": "Jakarta"
},
{
"id": "3",
"name": "Surabaya"
}
],
"status": [
"success"
]
}

You can test this service with Postman from https://dzkrrbb.com/tutorial/php-restapi/get_city.php

2. REST API — POST Method

This method usually used to create new resources on the REST server.

Now, we create file post_city.php and define the header like this :

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

This header will allow user to access service with POST method and get JSON data from the service.

Then, continue to set data :

$data = array(
array(
'id' => '1',
'name' => 'Bandung'
),
array(
'id' => '2',
'name' => 'Jakarta'
),
array(
'id' => '3',
'name' => 'Surabaya'
),
);

In this post, I use array data. But you can also set the data from database query.

And then, we need to create post function for input new data.

if(!empty($_POST['name']) && !empty($_POST['id'])) {// New Data Input
$newdata = array(
'id' => $_POST['id'],
'name' => $_POST['name']
);
// Add Data
$data[] = $newdata;
// New Data
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'] = 'success';
} else {
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'] = 'success';}

As you see in this function, we just add new data if we post id and name as a key to the REST. If we don’t post id and name, we just show the all data.

Finally, we store the result as JSON data.

http_response_code(200);
echo json_encode($result);

Now, we can test this service with Postman. For example, if we want to post new city with name “Solo” and id “4”, we can post in the body form-data using this key “id” with value “4” and “name” with value “Solo”. So, you can get the result like this :

{
"city": [
{
"id": "1",
"name": "Bandung"
},
{
"id": "2",
"name": "Jakarta"
},
{
"id": "3",
"name": "Surabaya"
},
{
"id": "4",
"name": "Solo"
}
],
"status": "success"
}

You can test this service with Postman from https://dzkrrbb.com/tutorial/php-restapi/post_city.php

3. REST API — PUT Method

This method usually used to update resources on the REST server.

Now, we create file put_city.php and define the header like this :

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: PUT");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

This header will allow user to access service with PUT method and get JSON data from the service.

Then, continue to set data :

$data = array(
array(
'id' => '1',
'name' => 'Bandung'
),
array(
'id' => '2',
'name' => 'Jakarta'
),
array(
'id' => '3',
'name' => 'Surabaya'
),
);

In this post, I use array data. But you can also set the data from database query.

And then, we need to create put function for replace data value.

$method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method) {
parse_str(file_get_contents('php://input'), $_PUT);
}
// Function Edit Data
if(!empty($_PUT['id']) && !empty($_PUT['name'])) {
foreach($data as & $value){
if($value['id'] === $_PUT['id']){
$value['name'] = $_PUT['name'];
break; // Stop the loop after we've found the item
}
}
// New Data
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'] = 'success';
} else {
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'] = 'success';
}

As you see in this function, we just replace data if we put id and name as a key to the REST. If we don’t put id and name, we just show the all data.

Finally, we store the result as JSON data.

http_response_code(200);
echo json_encode($result);

Now, we can test this service with Postman. For example, if we want to replace “Bandung” from the data, we can post in the body x-www-form-urlencoded using this key “id” with value “1” and “name” with value “Solo”. So, you can get the result like this :

{
"city": [
{
"id": "1",
"name": "Solo"
},
{
"id": "2",
"name": "Jakarta"
},
{
"id": "3",
"name": "Surabaya"
}
],
"status": "success"
}

You can test this service with Postman from https://dzkrrbb.com/tutorial/php-restapi/put_city.php

4. REST API — DELETE Method

This method usually used to delete resources from the REST server.

Now, we create file delete_city.php and define the header like this :

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

This header will allow user to access service with DELETE method and get JSON data from the service.

Then, continue to set data :

$data = array(
array(
'id' => '1',
'name' => 'Bandung'
),
array(
'id' => '2',
'name' => 'Jakarta'
),
array(
'id' => '3',
'name' => 'Surabaya'
),
);

In this post, I use array data. But you can also set the data from database query.

And then, we need to create delete function for delete data value.

$method = $_SERVER['REQUEST_METHOD'];
if ('DELETE' === $method) {
parse_str(file_get_contents('php://input'), $_DELETE);
}
// Function Edit Data
if(!empty($_DELETE['id'])) {
// New Data
foreach($data as $d) {
if($d['id'] != $_DELETE['id']) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
}
$result['status'] = 'success';
} else {
foreach($data as $d) {
$result['city'][] = array(
'id' => $d['id'],
'name' => $d['name'],
);
}
$result['status'] = 'success';
}

As you see in this function, we just delete data if we post id as a key to the REST. If we don’t put id and name, we just show the all data.

Finally, we store the result as JSON data.

http_response_code(200);
echo json_encode($result);

Now, we can test this service with Postman. For example, if we want to delete “Jakarta” from the data, we can post in the body x-www-form-urlencoded using this key “id” with value “1”. So, you can get the result like this :

{
"city": [
{
"id": "1",
"name": "Solo"
},
{
"id": "3",
"name": "Surabaya"
}
],
"status": "success"
}

You can test this service with Postman from https://dzkrrbb.com/tutorial/php-restapi/delete_city.php

Conclusion

The four methods listed above allow you to get, add, update, and delete data with REST API using PHP. You can download this REST service example on my Github https://github.com/dzikrirobbi/tutorial/tree/main/php-restapi

Thank you :D

--

--

Dzikri Robbi, S.Tp, M.Kom

Hi, I’m Dzikri Robbi, S.Tp, M.Kom. I’m a Founder & CEO RDPL, an Indonesian Creative Digital Technology Laboratory.