Monthly Archives: October 2013

Web Service Using PHP, MySQL, XML, and JSON for Beginners

Create a Basic RESTful Web Service Using PHP, MySQL, XML, and JSON.

Now a days web services are required with Mobile development.

Here I am going to show you how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.

Let me explain you what is API and Web Services.

Both API and Web service are way of transferring data with the concept of Request and Response.

  •  Web service is interacting between two machines over a network.
  •  An API works like interface between two different applications.

Web service types are SOAP, REST, and XML-RPC.

Lets understand what is Web Services first.

  • There are 3 major interface Client,Server,Database.
    • Client can be anything mobile,browser,script .In short who can make a request to server is client.
    • Server can be any machine that have facility to execute server pages.All the web services files are stored at server.
    • Database is storing the data provided by client or already filled in.
  • The process is well described in below picture.
web service call

web service call

REST – “Representational State Transfer”.

  • REST Web Services consist of using plain HTTP to make method calls and you get XML or JSON in response.
  • The RESTful way is to use GET, POST, PUT, and DELETE calls to implement CRUD (Create, Read, Update, Delete) operations on an object.
    • GET : Read
    • POST : Create
    • PUT : Update
    • DELETE : Delete
  • It accepts HTTP parameters, either in the query string request, or in the query string of a POST request.
  • REST style web services are what power AJAX applications. The browser makes a request to a REST web service via JavaScript’s XMLHttpRequest object. The browser then parses the xml and replaces elements in the page with the data.
  • REST is really quite simple. If you would like to connect to some REST Web Services below in article.

SOAP – “Simple Object Access Protocol”

  • Accessing a SOAP web service is super easy with any language that has SOAP libraries to help you.
  • All you have to do is point your code to the url of the web service’s WSDL, and you’re off and running, making remote calls as you would to a local object.
  • PHP5 has a SOAP library that will handle your client and server, but you have to create your own WSDL.

XML-RPC – “Remote Procedure Call uses XML”

  • XML-RPC protocol is really simple. You pass requests which contain the method name, and parameters formatted in xml that defines their data types. The response comes back with similar xml data.
  • XML-RPC servers are easier to implement than SOAP when using scripting languages such as php which have very loose data typing.
  • PHP-PEAR library that you use to handle XML-RPC client and server scripting.

You can submit request using browser with URL.

  • You can use URL parameters.

http//yourdomain/services.php?id=1&name=Jhon&email=abc@mail.com&method=Login

  • You can use JSON string as parameters.

http//yourdomain/services.php?data={“method”:”Login”,”id”:”1″,”name”:”Jhon”,”email”:”abc@mail.com”}

**- Make sure you need to fix one method from above,before start making web service.

Here we have created a skeleton for web services. Before moving, we need to get variable those are posted from client end.

Way 1(URL params) : we are checking the main method data , if not found then call custom function to prompt error to user.

Way 2(JSON string) : we are checking the main data variable and sub method params ,if not found then call custom function to prompt error to user.

Lets fetch data using above DB connection.

Here we are making database connection and executing query to fetch active user records.

You can change the query according to your table structure.

We are taking all the data and storing to a array.

“formatData” is function that accepts parameters array of data and output format.
function formatData will return data into JOSN or XML format.