Web Services are developed to interact different platforms in a single synchronized working environment uses XML to code and to interpret data, and SOAP to transport. With Web services you will be able to ex-change data between different apps, web, database and various platforms.
Now let us have a common database and we have requirement to update it day by day. We want to use this database with multiple web application and different platform. So we have decided to make a web service to access our common database with multiple platform and application.
Today I am writing for creating a simple web service which help you initiating for taking your first step to create a web service specially for PHP beginners.
What is a Webservice :-
Before start your first program you need to know what is web service?
A web service is a medium to communicate between two applications. It is a collection of functions, programs, messages etc. which return some information.
It is like a client server architecture that is based on request and response.
Steps to create a web service using SOAP client:-
1) Download NuSOAP libraries from site http://sourceforge.net/projects/NuSOAP/
NuSOAP (Nusphere PHP web service) is a set of php class where no php extensions are requires. It allows to create and consume/call a web service that are based on SOAP 1.1,WSDL 1.1 and HTTP 1.0/1.1.
2) Create a folder in www dir (if using wamp)
Wamp/www/webservice
3) Put NuSOAP library in this folder i.e.
Wamp/www/webservice/NuSOAP
4) Now create your server.php file which contain your functions or database script. Here we are just going to print simple This is my first web service. Save this file in NuSOAP folder.
Wamp/www/webservice/NuSOAP/server.php
Code: Server.php
<?php // include NuSOAP.php which contain SOAP client and server classes require_once ("lib/NuSOAP.php"); // create an object for SOAP_server class $server = new SOAP_server(); $server->configureWSDL("Testing WSDL ","urn:Testing WSDL "); //register your method/function i.e getwebservice here $server->register("getwebservice",array("name" => "xsd:string"),array("return" => "xsd:string"),"urn:webservice","urn:webservice#getwebservice"); //create function that return your information on client side function getwebservice($name) { $myname = "This is my <b>".$name . "</b>"; return $myname; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?>
5) Now test your server.php page
Run at your localhost
http://localhost/webservice/NuSOAP/server.php
You will see this window.
Now click on getwebservice. You will see this type of window.
Note: This is only for test your server.php file.
6) Now create client.php file
Wamp/www/webservice/NuSOAP/client.php
Code: client.php
<?php //include NuSOAP.php which contain SOAP client and server classes require_once ('lib/NuSOAP.php'); //create SOAP client object ,put your server file path $client = new SOAPclient("http://localhost/webservice/NuSOAP/server.php?wsdl"); //call method/function which is defined in our server page $result = $client->getwebservice("first webservice"); echo "<b>"; print_r($result); echo "</b>"; ?>
Now test your client.php
http://localhost/webservice/NuSOAP/client.php
You will see, your code is running successfully
Explore more PHP Snippets