Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,608 members, 7,809,225 topics. Date: Friday, 26 April 2024 at 06:00 AM

[php Tutorial] - Building A Rest Api - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / [php Tutorial] - Building A Rest Api (1579 Views)

Wordpress Experts, Please Help : Displaying Product Via A REST API / Php Tutorial For Beginners [live On On This Thread] / I Need PHP Tutorial Materials, Someone Should Please Help Me (2) (3) (4)

(1) (Reply) (Go Down)

[php Tutorial] - Building A Rest Api by Nobody: 9:52am On Oct 04, 2017
It's been long I shared anything, I've been busy doing things...but I just created time now to share with you guys how REST API development works, so after following this tutorial, REST API development shouldn't be new to you again and you can add it to your C.V.

[difficulty] - Advanced
[paradigm] - OOP (Object Oriented Programming)

Please and please, before taking this tutorial, make sure you have previous working experience with PHP and the OOP paradigm and you know how to perform basic CRUD operations.

I will cover the following
topics:
1.0 Overview
1.1 What is REST API?
1.2 Why do we need REST API?
1.3 Where is REST API used?

2.0 File Structure

3.0 Database setup
3.1 Table creation - Users table
3.2 Dump Data For Users table
3.3 Table creation - Posts table3.4 Dump Data For Posts table
3.5 Database connection

4.0 Read Blog post Data
4.1 Post Object
4.2 “read.php” file creation
4.3 Create Post > “read()”
method
4.4 Output

5.0 Create Post
5.1 "create.php" file creation
5.2 Create Post > "create()" method

6.0 Read One Post
6.1 "read_one.php" file creation
6.2 Create Post "readOne()" method
6.3 Output

7.0 Update posts
7.1 “update.php” file creation
7.2 Create Post > "update()" method

8.0 Delete Posts
8.1 “delete.php” file creation
8.2 Create Post > "delete()" method

9.0 Search Posts
9.1 “search.php” file creation
9.2 Create Post > “search()” method
9.3 Output

1.1 & 1.2 What is REST API and why do we need a RESTful API?
[Wikipedia]: Representational state transfer (REST) is a programming architectural implementation intended to increase the efficiency of communication in computing systems. It embodies the idea that the best way to share large amounts of data between multiple parties is to make that data available on-demand by sharing references to that data rather than a complete copy of the data itself. Systems which implement REST are called 'RESTful' systems.
https://simple.m.wikipedia.org/wiki/Representational_state_transfer

[Simple definition]:
REST is a simple concept for managing/sharing information over the Internet. These concepts are known as Resources and the representation of the resources must be stateless. The resources are usually represented via JSON (Pronounced Jason, Satnds for JavaScript Object Notation).
Now, API (Application Programming Interface) is a set of instructions that allows effective communication between pieces/portions of an application.
So from what we have above, a layman would deduce that a REST API is the stateless representation of resources through pieces of an application/external apps that interact with each other.
https://www.youtube.com/watch?v=7YcW25PHnAA&feature=youtu.be

[REST vs SOAP]
REST does not require processing and is more simple and flexible than SOAP (Simple Object Access Protocol), another standards-based Web services access protocol developed by Microsoft. REST is much easier and more widely used than SOAP (about 70% web services API uses REST API) and is the newcomer to the block which seeks to fix all problems with SOAP.

1.3 Where is REST API used?
Any app that creates, reads, updates or deletes data in a representational state using another app over the internet is definitely using a REST API.

2.0 File Structure
├─ api/
├─── config/
├────── core.php – used for core configuration
├────── database.php – used for database connection.
├─── objects/
├────── user.php – contains properties and methods for “user” database queries.
├────── post.php – contains properties and methods for “post” database queries.
├─── user/
├────── create.php – api user create endpoint.
├────── delete.php – api user delete endpoint.
├────── read.php – api user read endpoint.
├────── read_one.php – api user read single data endpoint.
├────── update.php – api user update endpoint.
├────── search.php – api user search endpoint.
├─── post/
├────── read.php – api post read endpoint.

3.0 Database setup
Our data needs to be stored somewhere to access them. Some may prefer other databases but for the sake of simplicity and general understanding i'd be using the relational database, MySQL (to be precise) through the MySqli driver.

3.1 Table creation - Users table


CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(17) NOT NULL,
`password` varchar(255) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;


3.2 Dump Data For Users table

INSERT INTO `categories` ( `id`, `username`, `password`, `created`, `modified` ) VALUES
( 1, 'tom', '96835dd8bfa718bd6447ccc87af89ae1675daeca', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
( 2, 'bola', '2ddb786aae2a2ad6506d1119ac86f1241a35b251', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
( 3, 'ahmed', '1698c2bea6c8000723d5bb70363a8352d846917e', '2014-06-01 00:35:07', '2014-05-30 17:34:54'),
( 5, 'isa', '59dc310530b44e8dd1231682b4cc5f2458af1c60', '2014-06-01 00:36:07', '2016-01-08 13:27:26' ),
( 6, 'buhari', 'e5c52b3c75ac4f1dfbee8ec994b696f6a83c6c51', '2014-06-01 00:39:07', '2016-01-08 13:27:47'),
( 13, 'seun', '6b4d1d408be6da222b02ed3c3426cc35a31f8f27', '2016-01-09 02:24:24', '2016-01-09 01:24:24') ;


[Note]: Password is = sha1 of username. (Used SHA1 encryption just for populating data, don't use it in production).




*this post will be constantly updated

3 Likes

Re: [php Tutorial] - Building A Rest Api by rayyspark(m): 10:14am On Oct 04, 2017
Just liking this...hoping to add something to my small brain
Re: [php Tutorial] - Building A Rest Api by Nobody: 10:19am On Oct 04, 2017
rayyspark:
Just liking this...hoping to add something to my small brain

I'm also looking forward to learning more from you guys. If you feel my code could be optimized or written in a better/more concise way please share the update with love.
Re: [php Tutorial] - Building A Rest Api by rayyspark(m): 10:39am On Oct 04, 2017
DanielTheGeek:


I'm also looking forward to learning more from you guys. If you feel my code could be optimized or written in a better/more concise way please share the update with love.


it's alright bro...observing and following up.
Re: [php Tutorial] - Building A Rest Api by kanayoNickel: 3:04pm On Oct 04, 2017
Quantastic...

Thank you
Re: [php Tutorial] - Building A Rest Api by Chukwudaalu(m): 6:54pm On Oct 04, 2017
smiley smiley
Re: [php Tutorial] - Building A Rest Api by Nobody: 8:20pm On Oct 04, 2017
Will still update with code references tho... you're welcome
kanayoNickel:
Quantastic...

Thank you
Re: [php Tutorial] - Building A Rest Api by Nobody: 12:42am On Oct 05, 2017
[updated]
Re: [php Tutorial] - Building A Rest Api by Nobody: 6:15pm On Oct 05, 2017
Will be nice at some point to add HATEOAS which makes a rest Api Self discoverable
Re: [php Tutorial] - Building A Rest Api by Nobody: 10:05pm On Oct 05, 2017
RaiH is beyond the scope of this post. But feel free to chip it in man.
Febup:
Will be nice at some point to add HATEOAS which makes a rest Api Self discoverable
Re: [php Tutorial] - Building A Rest Api by Nobody: 6:11pm On Oct 06, 2017
DanielTheGeek:
RaiH is beyond the scope of this post. But feel free to chip it in man.
No problem l will chip it in later
Re: [php Tutorial] - Building A Rest Api by heavykenny(m): 12:04pm On Oct 09, 2017
ride on bro

(1) (Reply)

Eye Pain When I Use My Computer, Phone And Watch Tv / As A Techie What State Do You Live And How Is Electricity In Your Area / Php

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.