Tuesday, 22 April 2014

Web Services For Mobile App iPhone or Android Using PHP And MYSQL



Web Services is a method for communicating two electronic devices over a single network.

XML + HTTP is the basic Web services platform.

Component of Web services:

1. SOAP (Simple Object Access Protocol)

2. UDDI (Universal Description, Discovery and Integration)

3. WSDL (Web Services Description Language)

Note: You can also use Json for Web Services



  1. Make Database and table as per need
  2. Write PHP Script using $_SERVER['REQUEST_METHOD']=="POST/PUT/GET/DELETE";
  3. Run PHP Script using localhost
  4. copy the url link of script page
  5. Open your Google Chrome and add extension "Advance Rest Client"
  6. Open Advance Rest Client
  7. Add localhost url and add it to Advance Rest Client Using Select Method(Eg : POST,PUT,GET,DELETE) and also Add the parameter of PHP Script in add new Value.
  8. Then click Send button in  Advance Rest Client it will show results that you web services is perfectly working or not.


Here you can find the code please click below
DEMO



DataBase Structure

1.

2.

3.

4.



For User SignUp

http://localhost/blog-ws/ws-server-regis/signup.php

signup.php

<?php
// Include confi.php
include_once('confi.php');
$sqlDate = date('Y-m-d H:i:s');

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']) : "";
$Email = isset($_POST['Email']) ? mysql_real_escape_string($_POST['Email']) : "";
$Country = isset($_POST['Country']) ? mysql_real_escape_string($_POST['Country']) : "";
$Company = isset($_POST['Company']) ? mysql_real_escape_string($_POST['Company']) : "";
$Gender = isset($_POST['Gender']) ? mysql_real_escape_string($_POST['Gender']) : "";
$DOB = isset($_POST['DOB']) ? mysql_real_escape_string($_POST['DOB']) : "";


$query = mysql_query("SELECT * FROM UserInfo Where email='$_POST[Email]'");
$query_row=mysql_fetch_array($query);

if($query_row == NULL)
{
    $sql = "INSERT INTO UserInfo (UserName, Password, Email, Country, Company, Gender, DOB, CreatedOn) VALUES 
('$UserName', '$Password', '$Email', '$Country', '$Company', '$Gender', '$DOB', '$sqlDate');";
$qur = mysql_query($sql);
//if($qur){
$json = array("status" => 1, "msg" => "Register Successfully!");
}else{
$json = array("status" => 0, "msg" => "Email Already Exits Use Another Email!");
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}

@mysql_close($conn);

/* Output header */
header('Content-type: application/json');
echo json_encode($json);


confi.php

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('exampleDB', $conn);


?>

For User Login

http://localhost/blog-ws/ws-server-regis/login.php

Login.php

<?php

include_once('confi.php');

if($_SERVER['REQUEST_METHOD'] == "POST"){


$Email = isset($_POST['Email']) ? mysql_real_escape_string($_POST['Email']) : "";
$Password = isset($_POST['Password']) ? mysql_real_escape_string($_POST['Password']) : "";




$query = mysql_query("SELECT * FROM UserInfo Where Email='$_POST[Email]' AND Password='$_POST[Password]'");
$query_row=mysql_num_rows($query);


if($query_row == 1)
{

 while($row=mysql_fetch_array($query)) {
 
  $json = array("status" => 1, "msg" => "Login Successfully!","UserID" => "$row[UserId]");
  }


}else{
$json = array("status" => 0, "msg" => "User Does not Exits!");
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}

@mysql_close($conn);

/* Output header */
header('Content-type: application/json');
echo json_encode($json);


?>

TO User Adding Note

http://localhost/blog-ws/Notes/notesend.php


Parameters :
NoteTypeID,UserID, NoteTitle, NotePath, Tag, Comments, Location, IsFavourites

<?php
$conn = mysql_connect("localhost", "root", "root");
mysql_select_db('exampleDB', $conn);
$sqlDate = date('Y-m-d H:i:s');
if($_SERVER['REQUEST_METHOD'] == "POST"){
$NoteTypeID = isset($_POST['NoteTypeID']) ? mysql_real_escape_string($_POST['NoteTypeID']) : "";
$UserID = isset($_POST['UserID']) ? mysql_real_escape_string($_POST['UserID']) : "";
$NoteTitle = isset($_POST['NoteTitle']) ? mysql_real_escape_string($_POST['NoteTitle']) : "";
$NotePath = isset($_POST['NotePath']) ? mysql_real_escape_string($_POST['NotePath']) : "";
$Tag = isset($_POST['Tag']) ? mysql_real_escape_string($_POST['Tag']) : "";
$Comments = isset($_POST['Comments']) ? mysql_real_escape_string($_POST['Comments']) : "";
$Location = isset($_POST['Location']) ? mysql_real_escape_string($_POST['Location']) : "";
$IsFavourites = isset($_POST['IsFavourites']) ? mysql_real_escape_string($_POST['IsFavourites']) : "";
$sql = "INSERT INTO Notes (NoteTypeID,UserID, NoteTitle, NotePath, Tag, Comments, Location, IsFavourites, CreatedOn) VALUES 
('$NoteTypeID','$UserID', '$NoteTitle', '$NotePath', '$Tag', '$Comments', '$Location', '$IsFavourites', '$sqlDate');";
$qur = mysql_query($sql);
if($qur){

$json = array("status" => 1, "msg" => "Your Note is Added Successfully!");
}else{
$json = array("status" => 0, "msg" => "Error In Adding Notes");
}
}else{
$json = array("status" => 0, "msg" => "Request Method not accepted Please Check Field!");
}
@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);
?>


TO Receive Information From Notes According To UserID

http://localhost/blog-ws/Notes/useridrecieve.php


useridrecieve.php

<?php
include_once("json.php");
$json = new Services_JSON();

$conn = mysql_connect("localhost", "root", "root");
mysql_select_db('exampleDB', $conn);

$query = "SELECT * FROM Notes";
$result = mysql_query($query);

$arr = array();
$rs = mysql_query("SELECT * FROM Notes where UserID='$_POST[UserID]'");

while($obj = mysql_fetch_assoc($rs)) {
$arr[] = $obj;
}

Echo $json->encode($arr);
//Echo '{:'.$json->encode($arr).'}';

?>


TO Receive Recent Tag And NoteTitle



http://localhost/blog-ws/Notes/recenttag.php



recenttag.php

<?php include_once("json.php"); $json = new Services_JSON(); $conn = mysql_connect("localhost", "root", "root"); mysql_select_db('exampleDB', $conn); $arr = array(); $rs = mysql_query("SELECT * FROM Notes Where UserID='$_POST[UserID]' order by CreatedOn desc"); while($obj = mysql_fetch_assoc($rs)) { $arr[] = "Recent Tag:".$obj['Tag']; $arr[] = "Recent Title:".$obj['NoteTitle']; } Echo $json->encode($arr); //Echo '{:'.$json->encode($arr).'}'; ?>



json.php





TO Forgot Password

http://localhost/blog-ws/Notes/forgotpassword.php

forgotpassword.php

<?php session_start();
include "connect.php"; //connects to the database
//if (isset($_POST['username'])){
if($_SERVER['REQUEST_METHOD'] == "POST"){

$Email = isset($_POST['Email']) ? mysql_real_escape_string($_POST['Email']) : "";
    //$username = $_POST['username'];
    $query="select * from UserInfo where Email='$_POST[Email]'";
    $result   = mysql_query($query);
    $count=mysql_num_rows($result);
    // If the count is equal to one, we will send message other wise display an error message.
    if($count==1)
    {
        $rows=mysql_fetch_array($result);
        $pass  =  $rows['Password'];//FETCHING PASS
        //echo "your pass is ::".($pass)."";
        $to = $rows['Email'];
        //echo "your email is ::".$email;
        //Details for sending E-mail
        $from = "ritu";
        $url = "http://qa.example.com/";
        $body  =  " Your Password is
        -----------------------------------------------
        Url : $url;
        email Details is : $to;
        Here is your password  : $pass;
        Sincerely,
        Rituraj";
        $from = "riturajkumar12@gmail.com";
        $subject = " Password recovered";
        $headers1 = "From: $from\n";
        $headers1 .= "Content-type: text/html;charset=iso-8859-1\r\n";
        $headers1 .= "X-Priority: 1\r\n";
        $headers1 .= "X-MSMail-Priority: High\r\n";
        $headers1 .= "X-Mailer: Just My Server\r\n";
        $sentmail = mail ( $to, $subject, $body, $headers1 );
    } else {
    if ($_POST ['Email'] != "") {
    //echo "<span style="color: #ff0000;"> Not found your email in our database</span>";
    $json = array("status" => 0, "msg" => "Not found your email in our database!");
        }
        }
    //If the message is sent successfully, display sucess message otherwise display an error message.
    if($sentmail==1)
    {
    $json = array("status" => 1, "msg" => "Your Password Has Been Sent To Your Email Address!");
        //echo "<span style='color: #ff0000;'> Your Password Has Been Sent To Your Email Address.</span>";
    }
        else
        {
     //   if($_POST['Email']!="")
        $json = array("status" => 0, "msg" => "Not found your email in our database!");
        //echo "<span style='color: #ff0000;'> Cannot send password to your e-mail address.Problem with sending mail...</span>";
    }
}
@mysql_close($conn);

/* Output header */

header('Content-type: application/json');
echo json_encode($json);
?>



TO Search Total Notes for Particular UserID

http://localhost/blog-ws/Notes/search.php

search.php

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('exampleDB', $conn);

if($_SERVER['REQUEST_METHOD'] == "POST"){
$UserID = isset($_POST['UserID']) ? mysql_real_escape_string($_POST['UserID']) : "";
$search = isset($_POST['search']) ? mysql_real_escape_string($_POST['search']) : "";
$search = htmlentities($search);

//$arr = array();
$rs = mysql_query("SELECT * FROM Notes Where UserID='$_POST[UserID]' AND NoteTitle LIKE '%$search%' order by UserID desc");
if($rs){
while($obj = mysql_fetch_assoc($rs)) {

$arr[] = "NoteTypeID :".$obj['NoteTypeID'];
$arr[] = "UserID :".$obj['UserID'];
$arr[] = "NoteTitle :".$obj['NoteTitle'];
$arr[] = "NotePath :".$obj['NotePath'];
$arr[] = "Tag :".$obj['Tag'];
$arr[] = "Comments :".$obj['Comments'];
$arr[] = "Location :".$obj['Location'];
$arr[] = "IsFavourites :".$obj['IsFavourites'];
$arr[] = "CreatedOn :".$obj['CreatedOn'];
$arr[] = "ModifiedOn :".$obj['ModifiedOn'];
/*$arr[]= array("status" => 1, "msg" => "Success!","NoteTypeID"=>"$obj[NoteTypeID]","UserID"=>"$obj[UserID]"
,"NoteTitle"=>"$obj[NoteTitle]","NotePath"=>"$obj[NotePath]","Tag"=>"$obj[Tag]"
,"Comments"=>"$obj[Comments]","Location"=>"$obj[Location]","IsFavourites"=>"$obj[IsFavourites]"
,"CreatedOn"=>"$obj[CreatedOn]","ModifiedOn"=>"$obj[ModifiedOn]");*/
}}
else{
$arr[] = array("status" => 0, "msg" => "Search Not Found!");
}
}else{
$arr[] = array("status" => 0, "msg" => "Request method not accepted");
}
@mysql_close($conn);

/* Output header */
header('Content-type: application/json');
echo json_encode($arr);
/*$sql = "INSERT INTO Notes (NoteTypeID,UserID, NoteTitle, NotePath, Tag, Comments, Location, IsFavourites, CreatedOn) VALUES 
('$NoteTypeID','$UserID', '$NoteTitle', '$NotePath', '$Tag', '$Comments', '$Location', '$IsFavourites', '$sqlDate');";
$qur = mysql_query($sql);
if($qur){
$json = array("status" => 1, "msg" => "Your Note is Added Successfully!");
}else{
$json = array("status" => 0, "msg" => "Error In Adding Notes");
}
}else{
$json = array("status" => 0, "msg" => "Request Method not accepted Please Check Field!");
}
@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);*/
?>
TO Retrieve All Information From Database  Table In Jason Using PHP And MYSQL

http://localhost/blog-ws/event/receive.php

receive.php

<?php
$conn = mysql_connect("localhost", "root", "root");
mysql_select_db('exampleDB', $conn);
if($_SERVER['REQUEST_METHOD'] == "POST"){
$sql = "SELECT * FROM Events";
$qur = mysql_query($sql);
if($qur){
while($row=mysql_fetch_object($qur)) {
$json[]=$row;
}}
}else{
$json = array("status" => 0, "msg" => "Request Method not accepted !");
}
@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);
?>


2 comments:

  1. I Have Been examining about this issue. So a commitment of thankfulness is all together to post. Totally cool post. It 's incredibly exceptionally OK and Useful post. Thanks
    php software developers

    ReplyDelete
  2. I wish more authors of this type of content would take the time you did to research and write so well. I am very impressed with your vision and insight. ajp150

    ReplyDelete

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...