Showing posts with label nusoap. Show all posts
Showing posts with label nusoap. Show all posts

Sunday, December 2, 2012

PHP SOAP Web Service With NuSOAP

nusoap

In this tutorial, we create a SOAP web service in PHP.  We use NuSOAP as a third party library for this tutorial. NuSOAP is a very useful library that eases SOAP  web service implementation.

It is a set of PHP classes - no PHP extensions required - that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1.
Download NuSOAP

Copy the lib folder to the project.

We use a simple function that performs zip code loookup as our web service.

First we create SOAP server which exposes the functionality for its clients.

zip_code_server.php

[sourcecode language="php"]
<?php

require_once 'lib/nusoap.php';

//Perform client authentication
//Unauthenticated clients are not allowed to access functionality
function doAuthenticate() {
if (isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['PHP_AUTH_PW'])) {

if ($_SERVER['PHP_AUTH_USER'] == "codezone4" && $_SERVER['PHP_AUTH_PW'] == "123")
return true;
else
return false;
}
}

//Retrieve zip code for textual area
//Logic of the web service
function get_zip_code($area) {
//Can query database and any other complex operation
if (!doAuthenticate())
return "Invalid username or password";
if ($area == "Temple Hills") {
return 20757;
} else if ($area == "Cheltenham") {
return 20623;
} else {
return "Can not find zip code for " . $area;
}
}

$server = new soap_server();
$server->configureWSDL("zipcodelist", "urn:zipcodelist");

//Register web service function so that clients can access
$server->register("get_zip_code",
array("area" => "xsd:string"),
array("return" => "xsd:int"),
"urn:zipcodelist",
"urn:zipcodelist#get_zip_code",
"rpc",
"encoded",
"Retrieve zip code for a given area");

$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($POST_DATA);
?>
[/sourcecode]

Now, run the server. You will see a link to wsdl file. Click that link and view the generated wsdl file. Clients who access the web service needs this wsdl. Save this file with .wsdl extension. You need to specify the URL for wsdl file within client.

zip_code_client.php

[sourcecode language="php"]
<?php
require_once('lib/nusoap.php');
$client = new nusoap_client("http://localhost/NuSOAP/zip_codes.wsdl", true);

$error = $client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
//Use basic authentication method
$client->setCredentials("codezone4", "123", "basic");
$result = "";

if ($client) {
$result = $client->call("get_zip_code", array("area" => "Cheltenham"));
}
if ($client->fault) {
echo "<h2>Fault</h2><pre>";
print_r($result);
echo "</pre>";
} else {
$error = $client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
} else {
echo "<h2>zip code</h2><pre>";
echo $result;
echo "</pre>";
}
}

echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>"
?>
[/sourcecode]

Download Project

Some additional stuff

How to use complex types?
//A complex method
function HelloComplexWorld($mycomplextype)
{
return $mycomplextype;
}

//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string')));

//Register our method using the complex type
$server->register(
// method name:
'HelloComplexWorld',
// parameter list:
array('name'=>'tns:MyComplexType'),
// return value(s):
array('return'=>'tns:MyComplexType'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Complex Hello World Method');

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...