registration api using php

In this article, we will discuss about web service or Rest API and create a simple Registration web service with the help of Php, MySQL, and JSON. API and Web service serve as a means of communication.

What is web service or Rest API ?

A web service is a way of communication between a client-server application or application component for communication. Web service is any piece of software or application that provides standardized web protocols (HTTP or HTTPS) to interoperate, communicate, and exchange data messaging.

Example-

Create a Database named as Test :

At first we create a Table named as user having six Columns in Test Database.

registration api using php

Create a Database Connection :

At first I am Connecting Php with Mysql Database

Config.php

<?php
define('hostname','localhost');
 define('user','root');
 define('password','root');
 define('db_name','Test');
?>

Here MySQL database user name is the root and the password is root and database name is Test. Now we create a connection file to Connect  the MySQL database and PHP

Con.php

<?php
 require_once 'config.php';
 class Connection{
   private $connect;
   function __construct(){
   $this->connect=mysqli_connect(hostname,user,password,db_name) or die("DB Connection error.");
   }
   public function get_connection()
   {
       return $this->connect;
    }
 }
       
 ?>

After this we are going create an another file named as register.php

<?php
 include('con.php');
 header('Content-Type: application/json');
 /**
 User Registeration
 */
 class Register
 {
 private $db;
 private $connection;
 function __construct()
 {
 //constructor call
 $this->db = new Connection();
 $this->connection=$this->db->get_connection();
 }
 public function does_user_exist($name,$mail,$mobile,$add,$pass)
 {
 // does user already exist or not
 $query = "SELECT * FROM user WHERE Email='$mail'";
 $result=mysqli_query($this->connection,$query);
 if(mysqli_num_rows($result)>0){
$json['status']=400;
$json['message']=' Sorry '.$mail.' is already exist.';
   echo json_encode($json);
   mysqli_close($this->connection);
 }else {
   $query="insert into user_details(name,email,mobile,address,password) values('$name','$mail','$mobile','$add','$pass')";
   $is_inserted=mysqli_query($this->connection,$query);
   if($is_inserted == 1){
$json['status']=200;
     $json['message']='Account created, Welcome '.$name;
   }else {
$json['status']=401;
     $json['message']='Something wrong';
   }
   echo json_encode($json);
   mysqli_close($this->connection);
 }
 } 
 }
 $register=new Register();
 if(isset($_POST['name'],$_POST['email'],$_POST['mobile'],$_POST['address'],$_POST['password']))
 {
   $name=$_POST['name'];
   $mail=$_POST['email'];
   $mobile=$_POST['mobile'];
 $add=$_POST['address'];
   $pass=$_POST['password'];
 if (!empty($name) && !empty($mail) && !empty($mobile) && !empty($add) && !empty($pass)) {
     $encrypted_password=md5($pass);
     $register-> does_user_exist($name,$mail,$mobile,$add,$encrypted_password);
   }else {
$json['status']=100;
     $json['message']='You must fill all the fields';
     echo json_encode($json);
   }
 }

?>

Now we can test it in Postman or any other rest client with the given key parameters name, email, mobile, address, and password. 🙂

Thanks a lot for reading…
Don’t forget to share this post if you found it interesting!
If you find something to add to this post? or any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback’s are most welcome.

%d bloggers like this: