Create REST API For Android App Using PHP and MySQL – Info PHP
Introduction
Application Programming Interface (API)
An API is good for communication between an app and a server. When we send a user request to the server using an Android server the response from your request is fast in JSON or XML format. REST is very simple compared to other methods like SOAP, CORBA and WSDL. The RESTful API supports the most commonly used HTTP methods (GET, POST, PUT and DELETE).
- GET to retrieve and search data
- POST to add data
- PUT to update data
- DELETE to delete data
How to Communicate Between a Device and MySQL Server using API
Start the Application
Step 1: Create a table in a MySQL database.
- CREATE TABLE products_api(
- pid int(11) primary key auto_increment,
- name varchar(100) not null,
- price decimal(10,2) not null,
- description text,
- created_at timestamp default now(),
- updated_at timestamp
- );
Step 2: Create two files called database_connect.php.
- define(‘DB_USER’, “root”);
- define(‘DB_PASSWORD’, “”);
- define(‘DB_DATABASE’, “myconnectapi”);
- define(‘DB_SERVER’, “localhost”);
- class DATABASE_CONNECT {
- function __construct() {
- $this->connect();
- }
- function __destruct() {
- $this->close();
- }
- function connect() {
- $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
- $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
- return $con;
- }
- function close() {
- mysql_close();
- }
- }
- ?>
- $MY_DB = new DB_CONNECT();
Insert product list API
Create an insert_product.php file. When the insert_proudct.php file is called and inserts a product the response is in JSON format.
insert_product.php
- $response = array();
- if (isset($_POST[‘name’$_POST[‘price’$_POST[‘description’])) {
- $name = $_POST[‘name’];
- $price = $_POST[‘price’];
- $description = $_POST[‘description’];
- require_once __DIR__ . ‘/ database_connect.php’;
- $db = new DB_CONNECT();
- $result = mysql_query(“INSERT INTO products_api(name, price, description) VALUES(‘$name’, ‘$price’, ‘$description’)”);
- if ($result) {
- $response[“success_msg”] = 1;
- $response[“message”] = “Product successfully Insert.”;
- echo json_encode($response);
- } else {
- $response[“success_msg “] = 0;
- $response[“message”] = “Product not insert because Oops! An error occurred.”;
- echo json_encode($response);
- }
- }
- ?>
The following output will be displayed in your browser.
- {
- ” success_msg “: 1,
- “message”: “Product successfully insert.”
- }
- {
- ” success_msg “: 0,
- “message”: ” Product not insert because Oops! An error occurred.”
- }
Reading product details
product_details.php
Deleting a Row in MySQL (Deleting a product)
delete_product.php
- $response = array();
- if (isset($_POST[‘pid’])) {
- $pid = $_POST[‘pid’];
- require_once __DIR__ . ‘/db_connect.php’;
- $db = new DB_CONNECT();
- $result = mysql_query(“DELETE FROM products WHERE pid = $pid”);
- if (mysql_affected_rows() > 0) {
- $response[“success”] = 1;
- $response[“message”] = “Product successfully deleted”;
- echo json_encode($response);
- } else {
- $response[“success”] = 0;
- $response[“message”] = “No product found”;
- echo json_encode($response);
- }
- }
- ?>
The output will be:
- {
- “success”: 1,
- “message”: “Product successfully deleted”
- }
Article Prepared by Ollala Corp