Wednesday, December 24, 2014

Restful webservice Application in PHP

Firstly let me be clear about the environment and requirements for the project. The required materials for the project are:

  1. IDE fully supportive for PHP
  2. A local Web Server (WAMP,XAMPP etc.) as per your O/S and easiness. 
  3. Google Chrome web browser with an extinction Advance rest Client.
Now if we have all this things with us we are ready to start. The basic functioning that our "Restfull Web Service " will be registering a user, loging a user along with some other functionalities that shall be brodened later.



Now lets create a database as per our requirement . Create a database "rest". Inside SQL insert the below code :

USE rest;
CREATE TABLE rest (username varchar(15) , password varchar (15), email varchar (50), status varchar (80));


The first thing we need to do is; create a folder "rest"  inside your web server after that lets create a file"DB_Config" that will enable us to connect to our database.

(DB_Config.php)

 <?php  
 /**  
  * Created by PhpStorm.  
  * User: wao  
  * Date: 1/26/2015  
  * Time: 4:28 PM  
  */  
 $mysql_hostname = "localhost"; /* replace it with your host [In most case the host name is same "localhost"] */  
 $mysql_user = "root"; /*It is a default username in your MySQL if you have set manual users please replace "root"*/  
 $mysql_password = "";/*It is a default password in your MySQL if you have set manual password please replace ""*/  
 $mysql_database = "rest"; /* insert your database name here*/  
 $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)  
 or die("Opps some thing went wrong, couldnot connect to the database");  
 mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong, cannot select database");  
 ?> 


After we are done with DB_Config.php file we will now create a next file namely "signup.php" 


 <?php  
 // Include confi.php  
 include_once('DB_Config.php');  
 if($_SERVER['REQUEST_METHOD'] == "POST"){  
  // This code gets data  
  $username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : "";  
  $email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";  
  $password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";  
  $status = isset($_POST['status']) ? mysql_real_escape_string($_POST['status']) : "";  
  // Below script inserts data into data base  
  $sql = "INSERT INTO `rest`.`users` (`ID`, `username`, `email`, `password`, `status`) VALUES (NULL, '$username', '$email', '$password', '$status');";  
  $qur = mysql_query($sql);  
  if($qur){  
  $json = array("status" => 1, "msg" => "Done User created!");  
  }else{  
  $json = array("status" => 0, "msg" => "Error creating user!");  
  }  
 }else{  
  $json = array("status" => 0, "msg" => "Request method not accepted");  
 }  
 @mysql_close($conn);  
 /* Output header */  
  header('Content-type: application/json');  
  echo json_encode($json);  
  ?>  

After finishing with "signup.php" we wil now create another file "login.php"


 <?PHP  
 include_once('DB_Config.php');  
 if($_SERVER['REQUEST_METHOD'] == "POST"){  
  // Get data  
  $username = isset($_POST['username']) ? mysql_real_escape_string($_POST['username']) : "";  
  $password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";  
  // Insert data into data base  
  $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";  
  $qur = mysql_query($sql);  
  $count=mysql_num_rows($qur);  
  if($count == 1)  
  {  
  $json=array("status"=>1, "msg"=>"user logged in");  
  }else{  
  $json=array("status"=>0, "msg"=>"user not loggedin!either password doesnot match or u have other error");  
  }  
 }else{  
  $json = array("status" => 0, "msg" => "Request method not accepted");  
  }  
 @mysql_close($conn);  
 /* Output header */  
  header('Content-type: application/json');  
  echo json_encode($json);  
  ?> 

     Open Google Chrome and run Advance Rest Client , and please refer to the snapshot for checking your webservice restfull application.


Download Source Code
    
restful web service php

No comments:

Post a Comment