Web Service Using PHP
Description :
Today i am going to explain you how to write a web service functionality and consume it using PHP code. It is very beginner level web service and gives you a clear idea about the Web Service using PHP.
What is Web Services ? :
Web services are a central part of modern software architecture. A web service is any piece of software that makes itself available over the internet and uses a standardized XML / RSS / JSON messaging systems.
What is SOAP ? :
SOAP is based on XML, it is a protocol for accessing a web service. It is simple XML based protocol to let applications exchange information over HTTP.
What is RSS ? :
RSS stads for Really Simple Syndication. It's an easy way for you to keep updates from different websites in a single application. Eg: You want to read leasted news from yahoo, google news ect, so instead of browsing all the sites you can build a simple application and request the RSS feeds from all other sites and display in one place.
What is JSON ? :
JSON stand for JavaScript Object Notation, JSON is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
Steps :
1) Create SimpleXMLElement object.
2) Add Rows to XML object.
3) Add Coloumns data to Rows of XML Object.
4) Create a DOM object using DOMDocument class.
5) Set the output Header Content-type: text/xml
server_xml.php :
[php]
<?php
$packages_data = new SimpleXMLElement("<packages></packages>"); //Registring SimpleXMLElement object & it's xml main node.
$package_id = htmlentities("0001", ENT_QUOTES,'UTF-8'); // htmlentities function used for escaping some text.
$package_name = htmlentities("SPLessons", ENT_QUOTES,'UTF-8');
$package_title = htmlentities("Simple Programming Lessons", ENT_QUOTES,'UTF-8');
$package_data = $packages_data -> addChild("package"); // add sub nodes to main <packages> node.
$package_data -> addChild("package_id",$package_id); // adding items to sub node.
$package_data -> addChild("package_name",$package_name); // adding items to sub node.
$package_data -> addChild("package_title",$package_title); // adding items to sub node.
$dom = new DOMDocument("1.0", 'UTF-8'); // Creating DOM element.
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($packages_data->asXML());
//send to browser
header("Content-type: text/xml"); // Specifing the out put Content-Type: text/xml
echo $dom->saveXML();
?>
[/php]
Steps :
1) Create RSS object using SimpleXMLElement("<rss versios='2.0'></rss>").
2) Add Rows to RSS object.
3) Add Coloumns data to Rows of RSS Object.
4) Create a DOM object using DOMDocument class.
5) Set the output Header Content-type: text/xml
server_rss.php :
[php]
<?php
$packages_data = new SimpleXMLElement("<rss versios='2.0'></rss>"); //Registring SimpleXMLElement object & it's xml main node.
$package_id = htmlentities("0001", ENT_QUOTES,'UTF-8');
$package_name = htmlentities("SPLessons", ENT_QUOTES,'UTF-8');
$package_title = htmlentities("Simple Programming Lessons", ENT_QUOTES,'UTF-8');
$package_data = $packages_data -> addChild("package"); // add sub nodes to main <packages> node.
$package_data -> addChild("package_id",$package_id); // adding items to sub node.
$package_data -> addChild("package_name",$package_name); // adding items to sub node.
$package_data -> addChild("package_title",$package_title); // adding items to sub node.
$dom = new DOMDocument("1.0", 'UTF-8'); // Creating DOM element.
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($packages_data->asXML());
//send to browser
header("Content-type: text/xml"); // Specifing the out put Content-Type: text/xml
echo $dom->saveXML();
?>
[/php]
Steps :
1) Create an array
2) Set the output Header Content-type: application/json
3) Out the put the Array uising json_encode function.
server_json.php :
[php]
<?php
$packages_data = array();
$packages_data["package_id"] = "0001";
$packages_data["package_name"] = "SPLessons";
$packages_data["package_title"] = "Simple Programming Lessons";
//send to browser
header("Content-type: application/json"); // Specifing the out put Content-Type: text/xml
echo json_encode($packages_data);
?>
[/php]
client.php :
[php]
<?php
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Content-type: application/x-www-form-urlencoded'
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost/webservices/server_xml.php', false, $context);
print_r($result);
?>
[/php]