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.

How to write a webservices using php with json format | Anil Labs

How to write a webservices using php with json format | Anil Labs

demo link for How to write a webservices using php with json format download link for How to write a webservices using php with json format

Web service for login method. Code is continue with user class which i mentioned in my previous post object oriented programming in php

Login Method Code

 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.

  <?php
/**********************************************************************
     * Author       :   Anil Kumar Panigrahi
     * E-mail           :   [email protected]
     * Created on       :   14th June 2011
     * Version      :   1.0
     * Project      :   Wevservices
     * Page         :   Login Webservice
     * Company          :   Anil Labs  (http://www.anillabs.com)
     * 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=anil&password=123456

Output:

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

complete documentation for this post is at in anther file download it for better understanding.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

9 Responses

  1. u2 says:

    How do I call the php webservice using jQuery mobile & display the json results ?

  2. Matteo Mosconi says:

    You should use Mysqli istead the old and deprecated method you are using in this tutorial.

  3. Sreekanth says:

    Can I call this webservice from html file just opened in a browser instead of calling http://localhost …etc, then only we can say it is a webservice

  4. Mithun says:

    Fatal error: Call to a member function bind_param() on boolean in

  1. June 18, 2011

    […] How to rectify json_encode returns null in php web services. In the previous post we know about How to write a webservices using php with json format . There is small issue with some special character return null when convert the string into […]

  2. August 13, 2013

    […] about the how to create documentation for php application. In the earlier post explained about how to write a webservices using php with json format. For that post I have written documentations for each and every code. […]

  3. October 3, 2013

    […] How to write a webservices using php with json format by Anil Kumar Panigrahi URL: http://www.anil2u.info/2011/06/how-to-write-a-webservices-using-php-with-json-format/ […]

  4. September 22, 2014

    […] http://www.anil2u.info/2011/06/how-to-write-a-webservices-using-php-with-json-format/ How to write a webservices using php with json format by Anil Kumar […]

Leave a Reply

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