Hello Friends today am going to tell you how to create a rest api in php for android app...
First of all, we need MySQL and Apache server installed in your pc, if they are installed in your pc then its very good but it is not installed in you pc/Laptop then you have to install them by clicking link blow the line:-
MySQL:-
https://dev.mysql.com/downloads/installer/
Apache Server:-
https://tomcat.apache.org/download-70.cgi
If you don't wanna download them then just installed Xampp in your pc
https://www.apachefriends.org/download.html
it is very helpful and no need to installed MySql and Apache server separately, Just installed it and start apache and MySql from it.
Now come to the Topic:-
if all done, we move to next step of creating rest api for this you have to use an PHP IDE or any text editor like notepad++.
This blog is written as you know about the syntax of PHP. and familiar with PHP Coding. for this firstly, we create a connection to MySQL to PHP. for this we can create a connection using MySQL, MySQLi and PDO Command. here we are focusing on MySQLi type.
For Connection use:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection$conn = new mysqli($servername, $username, $password);
// Check connectionif ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
define('DB_USER', 'root'); // root for local user
define('DB_PASSWORD', ''); // (mention your db password here)
define('DB_DATABASE', 'DBNAME'); // database name
define('DB_SERVER', 'SERVER');
$link = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
echo "There is a problem connecting to database";
}
now API part:
Hope you will get what do you want if you like please share it and help others...
First of all, we need MySQL and Apache server installed in your pc, if they are installed in your pc then its very good but it is not installed in you pc/Laptop then you have to install them by clicking link blow the line:-
MySQL:-
https://dev.mysql.com/downloads/installer/
Apache Server:-
https://tomcat.apache.org/download-70.cgi
If you don't wanna download them then just installed Xampp in your pc
https://www.apachefriends.org/download.html
it is very helpful and no need to installed MySql and Apache server separately, Just installed it and start apache and MySql from it.
Now come to the Topic:-
if all done, we move to next step of creating rest api for this you have to use an PHP IDE or any text editor like notepad++.
This blog is written as you know about the syntax of PHP. and familiar with PHP Coding. for this firstly, we create a connection to MySQL to PHP. for this we can create a connection using MySQL, MySQLi and PDO Command. here we are focusing on MySQLi type.
For Connection use:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection$conn = new mysqli($servername, $username, $password);
// Check connectionif ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Or use like this:
define('DB_USER', 'root'); // root for local user
define('DB_PASSWORD', ''); // (mention your db password here)
define('DB_DATABASE', 'DBNAME'); // database name
define('DB_SERVER', 'SERVER');
$link = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
echo "There is a problem connecting to database";
}
now API part:
<?php// get the HTTP method, path and body of the request$method = $_SERVER['REQUEST_METHOD'];$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));$input = json_decode(file_get_contents('php://input'),true);// connect to the mysql database$link = mysqli_connect('localhost', 'user', 'pass', 'dbname');mysqli_set_charset($link,'utf8');// retrieve the table and key from the path$table = preg_replace('/[^a-z0-9_]+/i','',array_shift($request));$key = array_shift($request)+0;// escape the columns and values from the input object$columns = preg_replace('/[^a-z0-9_]+/i','',array_keys($input));$values = array_map(function ($value) use ($link) { if ($value===null) return null; return mysqli_real_escape_string($link,(string)$value);},array_values($input));// build the SET part of the SQL command$set = '';for ($i=0;$i<count($columns);$i++) { $set.=($i>0?',':'').'`'.$columns[$i].'`='; $set.=($values[$i]===null?'NULL':'"'.$values[$i].'"');}// create SQL based on HTTP methodswitch ($method) { case 'GET': $sql = "select * from `$table`".($key?" WHERE id=$key":''); break; case 'PUT': $sql = "update `$table` set $set where id=$key"; break; case 'POST': $sql = "insert into `$table` set $set"; break; case 'DELETE': $sql = "delete `$table` where id=$key"; break;}// excecute SQL statement$result = mysqli_query($link,$sql);// die if SQL statement failedif (!$result) { http_response_code(404); die(mysqli_error());}// print results, insert id or affected row countif ($method == 'GET') { if (!$key) echo '['; for ($i=0;$i<mysqli_num_rows($result);$i++) { echo ($i>0?',':'').json_encode(mysqli_fetch_object($result)); } if (!$key) echo ']';} elseif ($method == 'POST') { echo mysqli_insert_id($link);} else { echo mysqli_affected_rows($link);}// close mysql connectionmysqli_close($link);Hope you will get what do you want if you like please share it and help others...