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

Web services are taking over the world. I credit Twitter’s epic rise to the availability of a simple but rich API. Why not use the same model for your own sites? Here’s how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.

The PHP / MySQL

With the number of persons hitting your web service (hopefully), you’ll need to do adequate validation before attempting to connect to the database to avoid injection attacks. Once we get the desired results from the database, we cycle through the results to populate our return results array. Depending upon the response type desired, we output the proper header and content in the desired format.

Take the following sample URL for example:

Now, we can take a look at the possible results of the URL.

The XML Output

Take this next sample URL for example:

Now, we can take a look at the possible results of the URL.

The JSON Output

Creating a basic web service is very simple and encourages your users to spread the word about your website or service. Want more traffic? Want your website to grow without you putting in all the effort? Create a web service!

Leave a Reply

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

CAPTCHA