Rest API And HTTP Methods (GET, POST, PUT, DELETE) Using Slim Framework And PHP

On this occasion I would like to share how to create, use and calling REST API on Slim Framework and PHP. This article is a continuation of my previous article “a basic introduction to slim framework“. Rest API associated with the http request method such as GET, POST, PUT and DELETE which the client will make a request to the web service.

An application usually only perform CRUD (Create, Read, Update, Delete). In the Rest API, CRUD functions in accordance with the 4 HTTP method i.e., POST, GET, PUT, and DELETE. So we can choose the right method for REST API.

Rest API And HTTP Methods (GET, POST, PUT, DELETE) Using Slim Framework And PHP

In this article I will make a simple rest API to retrieve the data in JSON format. The application will perform the request against the rest api service that was created using the Slim Framework and accessed via the PHP CURL.

In order to Service Rest API is good and easy to understand, the Developer must specify the raw data format and consistent. So a client with easy access and study the API we will create. For example this tutorial, any request will return the JSON format is as shown below

Json Format Response Simple Rest Api Using Slim Framework

In this example, the API request on PHP using CURL functions. PHP provides functions to perform the curl request using DELETE and PUT method.

Here is a simple program to make the register form, login form, edit form, delete form and index page that applications will apply the http method ( GET, POST, PUT, and DELETE) using PHP CURL to perform a simple REST API request using Slim Framework.

Simple Rest Api Using Slim Framework and Curl PHP

1. Create a project folder with name “restapi
2. Install the slim framework using the composer with the following script:

composer require slim/slim “^3.0”

3. Make sure your folder structure as shown below

Restapi Folder Project

4. Create 6 pieces PHP files in the folder example

  • login.php
  • delete.php
  • index.php
  • edit.php
  • register.php
  • logout.php

Folder “example” used to call the REST API service

login.php

Description :

The login page will implement PHP curl to make requests with the GET method.

register.php

Description :

The register page will implement PHP curl to make requests with the POST method.

index.php

edit.php

Description :

The edit page will implement PHP curl to make requests with the PUT method.

delete.php

Description :

The delete page will implement PHP curl to make requests with the DELETE method.

logout.php

5. If all of the above files is completed, the next step we will make the simple REST API code in the “api” folder. Add 2 pieces of files index.php and .htaccess in the “api” folder and copy the following code

index.php

Description:

To answer the request in a json format on Slim Framework using the following functions:

Create a handler if request is not found(404) in slim framework 3.0 using the following functions:

.htaccess

Description :
The above script is used to create pretty links

Save and finish, the result will be as shown below

Simple Example How To Create Rest Api And Use Http Method With Slim Framework By Seegatesite.com

Thus my tutorial, please download the complete script on the links below to direct to practice, because I’m not good in explaining 🙂

Download project : http://wp.me/a65Dpx-H9

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Latest posts by Sigit Prasetya Nugroho (see all)

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.