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

<?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->Content) &amp;&amp; isset($xml->Sender) &amp;&amp; isset($xml->DateTime)){

//die;
$message=$xml->Content;
$sender=$xml->Sender;
$msg_date=$xml->DateTime;

......

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

?>

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

If you enjoyed this article, Get email updates (It’s Free)
http://w3lessons.info/wp-content/uploads/2013/02/PHP-Web-Services-API-1024x785.jpghttp://w3lessons.info/wp-content/uploads/2013/02/PHP-Web-Services-API-150x150.jpgSasikumarPHPwebservice
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...