How to write a webservices using php with json format


How to write a webservices using php with json format

Hi friends, In this post i would like to explain about how to write a web services using php with the json format. Now a days mobile applications are more popular. Most of mobile applications are implementing for existing websites. To isplay information from database to mobile one mediator is require. So that mediator implementing using php and the output as JSON format to easy to retrieve.

 

Web service for login method.

Login Method Code

1
2
3
4
5
6
 function login($username,$password){
      $query = "select * from users where username='".$username."' AND password='".$password."'";
      $result = mysql_query($query);
      $num_rows = mysql_num_rows($result);
      return $num_rows;
 }

In the web service page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  <?php 
/**********************************************************************
     * Author       :   Nilesh Pawar
     * E-mail           :   pawarnilesh4u@gmail.com
     * Created on       :   14th June 2011
     * Version      :   1.0
     * Project      :   Wevservices
     * Page         :   Login Webservices
     * Modified on      :   
     * Modified by      :   
*************************************************************************/

// Includes the class which have the all functions
ini_set('display_errors', '1');
include_once("user.class.php");

/* Created object for User class*/
$web = new users();
/* This file return the sucess or failure with provided details ( username, password ) */

// USER NAME - user_name
// PASSWORD - password


if(isset($_REQUEST) && ($_REQUEST['user_name'] != "") && ($_REQUEST['password']!="") ){

    $result = $web->login($_REQUEST['user_name'],$_REQUEST['password']);
    echo json_encode(array('results'=>$result));
}

/* Provided output in the json encoded format */
// Call the database disconnection method
$web->connection_close();

?>

Call this method as

http://localhost/login.php?username=nilesh&password=123456

Output:

if correct {“result”:1} else {“result”:0}

No comments

Enter your email address:

Delivered by FeedBurner

OR

 Subscribe in a reader

 
Latest Blogs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Tips for optimizing php code by PHP Expert Important SERVER Variables in PHP - By PHP Expert Improved Error Messages in PHP 5 - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert Object Overloading in PHP 5 Persistable Classes - By PHP Expert Dynamic Getter/Setter Methods - By PHP Expert New Functions in PHP 5 New Directives - By PHP Expert Exception Handling - By PHP Expert Password Encryption in PHP - By PHP Expert Output Buffering in PHP - By PHP Expert Page Excerpts Using CURL - By PHP Expert Quick and Easy Google Site Search - By PHP Expert Always Be Notified When Google Crawls Your Site - By PHP Expert How to POST Form Data using CURL - By PHP Expert Cryptography for web developers - By PHP Expert Dynamically Loading JavaScript Files - by PHP Expert What is Web 2.0 - By PHP Expert