PHP MySQLi RESTful API with Slim Framework

PHP MySQLi RESTful API with Slim Framework

Today, We want to share with you PHP MySQLi RESTful API with Slim Framework.
In this post we will show you Creating RESTful API with Slim Framework,PHP MySQL, hear for how to create rest api for android app using php, slim and mysql we will give you demo and example for implement.
In this post, we will learn about How to Create a Simple REST API With Slim Framework with an example.

Slim is a list of more full-featured,and as well as open-source PHP micro framework used that helps you easy and quickly write simple yet dynemically powerful web applications and android APIs. only require slim class to It comes with a sophisticated URL dispatcher and middleware simple and more useful and also stored cookies and session architecture that makes it ideal simple to largest used for static websites or API prototyping.and It supports all HTTP methods like GET, POST, PUT and DELETE using Query the HTTP methods.Creating RESTful API with Slim Framework,PHP/MySQl

Another must read:  Redirect from one website url to another url

Simple PHP REST API with Slim, PHP & MySQL Example

index.php – RestAPI for PHP Configuration – Database Configuration

 true,
		'debug' => true,
		'cookies.lifetime' => '45 minutes',
		'cookies.path' => '/',
		'cookies.domain' => 'pakainfo',
		'cookies.secure' => false,
		'cookies.httponly' => false,
		'cookies.secret_key' => 'secret'
	)
);

error_reporting(0);
//start user api function
$app->get('/viewName/:name', viewnamef);

$app->post('/creategroup',get_creategroup);
$app->get('/viewName/:name', viewnamef);
$app->get('/getuserdata/:mobile/:deviceid/:device_token',get_userdata);
// *********************START Default credentials******************************
//Login Logout Calls
$app->post('/login', 'LoginDetails');
$app->post('/login1', 'LoginDetails1');
$app->get('/logout', 'Logout');
//$app->get('/monitor/:type', 'getMonitorDetails');

$app->contentType('application/json');
//$app->call()
$app->run();

function getConnection()
{
    $dbhost="localhost";
    $dbuser="slimclassapi";
    $dbpass="admin%12399";
    $dbname="webrest";
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);	
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}
// END Default credentials
?>

function.php

prepare($sql);
	$sqlQQ->execute();
	
	$data=array();
	$data['response']="success";
	$data['data']=array();
	echo json_encode($data);

		
	}
	catch(PDOException $e)
	{
		echo '{"Response":"0","ERROR":"Please Enter Correct Data"}';
		echo $d1 = '{"error":{"text":' . $e->getMessage() . '}}';
		wh_log("Details" . $d1);
	}
}
function get_userdata($mobile,$deviceid,$dtoken)
{
	$data = array();
	$data['response'] = "success";
	$data['data'] = array();
	
			$d=htmlspecialchars_decode($dtoken);
			$dtokenf=addslashes($d);
				
							
		try
		{
			$mobilCheck = "SELECT * FROM usermst where usermobile='$mobile'";
			$db = getConnection();       
			$dtoken = $db->prepare($mobilCheck);
			$dtoken->execute();
			$selectno = $dtoken->fetchAll(PDO::FETCH_ASSOC);
			$mcount = count($selectno);
							$userids= array();
							$status=array();
	
			
			if($mcount == 0)
			{
	
	
			
				$dt=date('Y-m-d H:i:s');
				wh_log("########## START[IN FUNCTION get_userdata($mobile,$deviceid,$dtoken))]'" . date('d-m-Y H:i:s') . "'##########");
				$sql="INSERT INTO `usermst` (`userid`, `usermobile`, `regdt`, `status`, `deviceid`, `device_token`) VALUES (NULL, '$mobile', '$dt', '0', '$deviceid', '$dtokenf')";
				 
					wh_log("sqlquery" . $sql);
					$db = getConnection();
					$sqlQQ =  $db->prepare($sql);
					$sqlQQ->execute();
					$new_userid = $db->lastInsertId();

					
					
						$getuid = "SELECT userid,status FROM usermst where userid='$new_userid'";
						$db = getConnection();       
						$res = $db->prepare($getuid);
						$res->execute();
						$selectno = $res->fetchAll(PDO::FETCH_ASSOC);
					foreach($selectno as $dt)
					{
						$userids[] = $dt['userid'];
						$status[] = $dt['status'];
					}
					$userid=implode(",",$userids);
					$status=implode(",",$status);
				
					$data['data']['userid'] =$userid;	
					$data['data']['status'] =$status;
					
					echo json_encode($data);
			}
			else if($mcount == 1)
			{
					$getuidupdate = "update usermst SET deviceid='$deviceid',device_token='$dtokenf' where usermobile='$mobile'";
					
						$db = getConnection();       
						$resupdate = $db->prepare($getuidupdate);
						$resupdate->execute();
				
					$getuid = "SELECT userid,status FROM usermst where usermobile='$mobile'";
					
						$db = getConnection();       
						$res = $db->prepare($getuid);
						$res->execute();
						$selectno = $res->fetchAll(PDO::FETCH_ASSOC);
					
					foreach($selectno as $d)
					{
						$userids[] = $d['userid'];
						$status[] = $d['status'];
					}
					$userid=implode(",",$userids);
					$status=implode(",",$status);
				
					
					$data['data']['userid'] =$userid;	
					$data['data']['status'] =$status;
					echo json_encode($data);
			}
		}
			catch(PDOException $e)
	{
		echo '{"Response":"0","ERROR":"Please Enter Correct Data"}';
		echo $d1 = '{"error":{"text":' . $e->getMessage() . '}}';
		wh_log("Details" . $d1);
	}
	
}
function wh_log($msg)
{
	$logfile = 'log/log_' . date('d-M-Y') . '.log';
    file_put_contents($logfile, $msg . "\n", FILE_APPEND);
}

Following List of PDO Query

HTTP methods like GET, POST, PUT and DELETE using Query

Another must read:  Angularjs 6 User Registration and Login Authentication

update Query

prepare($getuidupdate);
$resupdate->execute();
?>

SELECT Query

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

prepare($deviceT);
$deviceTStmnt->execute();
$deviceTData = $deviceTStmnt->fetchAll(PDO::FETCH_ASSOC);
$deviceTCount = count($deviceTData);
$device_id= array();
if($deviceTCount > 0)
{
	foreach($deviceTData as $dt)
	{
		$device_id[] = $dt['device_token'];
	}
}
?>

INSERT Query

prepare($sqlquery);
$sqlQQ->execute();
$data['response'] = "success";
$data['msg'] = "You are add in $gname";
echo json_encode($data);
?>

DELETE Query

query($partySS);
		echo json_encode($data);
	
		wh_log("Details = " . json_encode($data));
	}
	catch(PDOException $e)
	{
		echo '{"Response":"0","ERROR":"Please Enter Correct Data"}';
		echo $d1 = '{"error":{"text":' . $e->getMessage() . '}}';
		wh_log("Details" . $d1);
	}
	
}
?>

Example

We hope you get an idea about PHP MySQLi RESTful API with Slim Framework
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Reply

Your email address will not be published. Required fields are marked *