How to build a simple web service API in php

People are looking for a wide variety of data in web sites which means most of the web applications today are not a single stand alone application.  Web services come into picture when different applications need to link their data. For example your web site can publish its data to rest of the world via a web service and the other sites can act as the consumers of the data.

PHP Web Services API
PHP Web Services API

Now lets take a simple example to see how you can write a basic web service api to consume data POSTed by a third party.

Consider a scenario in which a  website say cricinfo needs to consume data from a sms service provider. Whenever a match is happening users send sms via this sms service and this sms should be shown in cricinfo. How does this happen?

Step 1 : The sms service provider and cricinfo would sign a contract. The contract makes clear with both the producer and consumer of the service exactly what is needed and what is expected. This is a contract-first approach however there is another approach called contract-last which is not recommended.

The sms provider would say I would be posting a xml(json is another option) data in the following format

<Message>

<Content>blah</Content>

<Sender>9676546345</Sender>

<DateTime>2013-01-21 19:35:21</DateTime>

Step 2 : Now the consume,r in our case ,cricinfo would start building the web service to consume the xml in the format provided.

The code snippet in php would be something like this

&amp;amp;lt;?php
//Reads the posted xml

$xml_post = file_get_contents('php://input');

if ($xml_post) {
$xml_file = 'received_xml_' . date('Y_m_d-H-i-s') . '.xml';
$fh = fopen($xml_file, 'w') or die();
fwrite($fh, $xml_post);
fclose($fh);
return;
}

if(file_exists($xml_file)){
//load xml file
$xml = simplexml_load_file($xml_file);

if(isset($xml-&amp;amp;gt;Content) &amp;amp;amp;amp;&amp;amp;amp;amp; isset($xml-&amp;amp;gt;Sender) &amp;amp;amp;amp;&amp;amp;amp;amp; isset($xml-&amp;amp;gt;DateTime)){

//die;
$message=$xml-&amp;amp;gt;Content;
$sender=$xml-&amp;amp;gt;Sender;
$msg_date=$xml-&amp;amp;gt;DateTime;

......

/* whatever you want to do with the web service data */

?&amp;amp;gt;

Step 3: The php file is hosted in the cricinfo site and the url www.espncricinfo.com/receive_sms.php (here receive_sms.php is the php code that we saw above) is given to the sms service provider

Step 4 : Now the sms service provider posts the actual data. This can be done in a number of ways. But the most common way is using curl.

image

Thats it !! We are done. But the point to be noted is xml can not be posted raw since it wont accept special characters. So its always preferred to encode the xml using CDATA

You May Also Like

Never Miss Any Web Tutorials, Guides, Tips and Free eBooks

Join Our Community Of 50,000+ Web Lovers and get a weekly newsletter in your inbox

 

I hate spam too. Unsubscribe at any time.

1 thought on “How to build a simple web service API in php”

  1. You didn’t build web service API rather you have created a client application or program to access information from web service API (SMS provider). so please fix the title of this post.

    Reply

Leave a Comment