How to create Web Service in PHP

Web Services can convert your application into a platform independent Web-application. You can call web services using mobile applications (e.g iPhone, BB or android). The basic Web Services platform is XML & HTTP. To create web services you should know XML & HTML.

What are Web Services?
- Web services are application components
- Web services communicate using open protocols
- Web services are self-contained and self-describing
- Web services can be discovered using UDDI (Universal Description, Discovery and Integration)
- Web services can be used by other applications

Web services platform elements are as follows:
- SOAP (Simple Object Access Protocol)
- UDDI (Universal Description, Discovery and Integration)
- WSDL (Web Services Description Language)

Creating web service in PHP:

Using NuSoap library you can create web service in PHP.

Step1. WSDL configuration :

require_once(“nusoap.php”);
$ns=”http://192.168.1.1/nusoap”;
$server = new soap_server();
$server->configureWSDL(‘test’,$ns);
$server->wsdl->schemaTargetNamespace=$ns;

Step2. Register method :

$server->register(‘test’,array(‘amount’ => ‘xsd:string’),array(‘return’ => ‘xsd:string’),$ns);

Step3. Writing PHP function :

function test($amount) {
if($_SERVER['PHP_AUTH_USER']==”test” and $_SERVER['PHP_AUTH_PW']==”supp0rt”) // check for user &
passwd $msg = “valid user”;
else $err = “Invalid user”; // XML to return response
$strOP = “”; $strOP .= “”;
$strOP .= “”.htmlentities($err).”";
$strOP .= “”.htmlentities($msg).”";
$strOP .= “”.htmlentities($amount).”";
$strOP .= “”; // eof XML to return response return $strOP;// return response }

 

Step4. SOAP response :

// send the result as a SOAP response over HTTP if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents( ‘php://input’ ); $server->service($HTTP_RAW_POST_DATA);

 

Originally posted 2011-08-15 12:57:23.

This entry was posted in PHP and tagged , , , , , . Bookmark the permalink.

Leave a Reply