Creating Web Service Using PHP Within 10 Minutes
Thanks for share. Ref;
What is SOAP?
SOAP is based on XML so it is considered human read. It is a protocol for accessing a Web Service. It is a simple XML-based protocol to let applications exchange information over HTTP.
Why SOAP?
In real field, so many applications are required for data communication between systems by Remote Procedure Calls (RPC) between objects like DCOM and CORBA but HTTP was not designed for this.
- RPC represents compatibility
- Security problem
- Firewalls and proxy servers will normally block this kind of traffic.
A better way to communicate between applications is over HTTP as HTTP is supported by all Internet browsers and servers. That's why it is preferable to SOAP Service.
We can collaborate with other programmers building big size and complex applications in multiple platforms.
What is SOAP?
SOAP is based on XML so it is considered human read. It is a protocol for accessing a Web Service. It is a simple XML-based protocol to let applications exchange information over HTTP.
Why SOAP?
In real field, so many applications are required for data communication between systems by Remote Procedure Calls (RPC) between objects like DCOM and CORBA but HTTP was not designed for this.
- RPC represents compatibility
- Security problem
- Firewalls and proxy servers will normally block this kind of traffic.
A better way to communicate between applications is over HTTP as HTTP is supported by all Internet browsers and servers. That's why it is preferable to SOAP Service.
We can collaborate with other programmers building big size and complex applications in multiple platforms.
Here, I will create a web service using PHP code. So let’s follow the steps given below:
Step 2: Run WAMP server, then you will go to www root folder location.
Step 3: Create folder, it’s called “WebServiceSOAP” into www root folder.
Step 4: Paste "lib" folder inside your "www/WebServiceSOAP/" location from Step 1 download files.
Step 5: Create two files “server.php” and “client.php” into WebServiceSOAP folder location.
Step 6: Inside “server.php”, please write the code lines given below:
Collapse | Copy Code
<?php
require_once ('lib/nusoap.php');
$server = new soap_server;
$server->register('get_message');
function get_message($your_name)
{
if(!$your_name){
return new soap_fault('Client','','Put Your Name!');
}
$result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
Step 8: Create "client.php" file into WebServiceSOAP folder location. Please find the following code lines:
Collapse | Copy Code
<?php
require_once ('lib/nusoap.php');
$param = array( 'your_name' => 'Monotosh Roy');
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
$response = $client->call('get_message',$param);
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
Collapse | Copy Code
Result: Welcome to Monotosh Roy. Thanks for Your First Web Service Using PHP with SOAP
So, today I have shown you how to use simple SOAP web service and to get message from service at client end.
Hope this post will help you to create a simple web service using SOAP. Post your queries if you have any or if there is anything else, please share. Keep visiting for new posts here.
Have a good day!