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

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       :   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.


11 Comments

u2 · September 1, 2011 at 5:39 pm

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

MohanKumar S · October 19, 2011 at 5:13 am

@U2 I wish you have a look into
http://tinywall.info/2011/10/19/facebook-twitter-like-json-feeds-with-php-javascript-ajax-jquery-mysql/ for fetching Info and use it in jquery mobile too.

Matteo Mosconi · September 1, 2012 at 9:09 am

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

Sreekanth · March 14, 2013 at 6:39 am

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

Mithun · May 18, 2016 at 9:51 am

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

How to rectify json_encode returns null in php web services | ANIL KUMAR PANIGRAHI 's Blog · June 18, 2011 at 5:54 am

[…] 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 […]

Webservices using PHP with JSON format Documentation - Anil Labs · August 13, 2013 at 7:22 pm

[…] 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. […]

Top blog posts of Anil Labs in September - Anil Labs · October 3, 2013 at 1:37 pm

[…] 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/ […]

Top blog posts in 2013 - Anil Labs · September 22, 2014 at 5:45 am

[…] 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 […]

How to call API in CakePHP - Anil Labs · October 24, 2018 at 9:15 am

[…] post explains how to call API in CakePHP, in earlier we learn about webservices, documentation for webservices and how to create API in Magento. In the previous post we learn […]

Pretty printing of JSON using PHP and Javascript - Anil Labs · November 8, 2018 at 6:47 am

[…] to explain how to display JSON data in a pretty way. In the earlier posts, I have explained how to create API. This post is extended to display JSON, so that easy to understand and readable. To print the JSON […]

Leave a Reply

Avatar placeholder

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