What is api in php with example

Simple REST API With PHP MySQL (Step-by-Step Example)

Welcome to a tutorial and example on how to create a simple REST API in PHP and MYSQL. The term “REST API” probably sounds intimidating to some beginners, but in actual fact, it is something really simple.

In layman’s terms, a REST API is nothing but a service endpoint. It simply accepts a user request, processes it, and responds with the results. For example, we send $_POST[«email»]=»jon@doe.com» and $_POST[«password»]=»123456″ to https://site.com/api/login/ . The system processes the login request and replies OK .

Yep, that’s all. Read on for a detailed example!

TLDR – QUICK SLIDES

Simple REST API In PHP MySQL

TABLE OF CONTENTS

REST API DEVELOPMENT

All right, let us now get into a simple example of creating a user API in PHP and MYSQL.

STEP 1) DUMMY USER DATABASE TABLE

CREATE TABLE `users` ( `user_id` bigint(20) NOT NULL, `user_email` varchar(255) NOT NULL, `user_password` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ALTER TABLE `users` ADD PRIMARY KEY (`user_id`), ADD UNIQUE KEY `user_email` (`user_email`); ALTER TABLE `users` MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT;

STEP 2) PHP USER LIBRARY

pdo = new PDO( "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, DB_USER, DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); > // (B) DESTRUCTOR - CLOSE DATABASE CONNECTION function __destruct () < if ($this->stmt!==null) < $this->stmt = null; > if ($this->pdo!==null) < $this->pdo = null; > > // (C) SUPPORT FUNCTION - SQL QUERY function query ($sql, $data) : void < $this->stmt = $this->pdo->prepare($sql); $this->stmt->execute($data); > // (D) CREATE/UPDATE USER function save ($email, $pass, $id=null) < $data = [$email, password_hash($pass, PASSWORD_BCRYPT)]; if ($id===null) < $this->query("INSERT INTO `users` (`user_email`, `user_password`) VALUES (. )", $data); > else < $data[] = $id; $this->query("UPDATE `users` SET `user_email`=?, `user_password`=? WHERE `user_id`=?", $data); > return true; > // (E) DELETE USER function del ($id) < $this->query("DELETE FROM `users` WHERE `user_id`=?", [$id]); return true; > // (F) GET USER function get ($id) < $this->query("SELECT * FROM `users` WHERE `user_".(is_numeric($id)?"id":"email")."`=?", [$id]); return $this->stmt->fetch(); > // (G) VERIFY USER (FOR LOGIN) function verify ($email, $pass) < // (G1) GET USER $user = $this->get($email); if (!is_array($user)) < return false; >// (G2) PASSWORD CHECK if (password_verify($pass, $user["user_password"])) < $_SESSION["user"] = [ "id" =>$user["user_id"], "email" => $user["user_email"] ]; return true; > else < return false; >> > // (H) DATABASE SETTINGS - CHANGE TO YOUR OWN! define("DB_HOST", "localhost"); define("DB_NAME", "test"); define("DB_CHARSET", "utf8mb4"); define("DB_USER", "root"); define("DB_PASSWORD", ""); // (I) START! session_start(); $USR = new Users();
Оцените статью