Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5552

Now, create your own API using NuSOAP

$
0
0

NuSOAP (formerly SOAPx4) is a toolkit which provides simple API for building web services using SOAP.NuSOAP current version is 0.6.4, which provides simple API for making SOAP server/client applications and also supports features like WSDL generation, building the proxy class, using SSL, using HTTP proxy, HTTP authentication.

Building APIs usingNuSOAP is damn easy. You just need to follow steps mentioned below:

1. Downloading the NuSOAP

First, download NuSOAP from here: http://sourceforge.net/projects/nusoap/

2. Build the server

Creating server using NuSOAP API is not a rocket science. You just need to import library file nusoap.php and other things can be understood from the code below.

<?php

	require_once('../lib/nusoap.php');

	$server = new nusoap_server;

	$server->configureWSDL('opencart', 'urn:opencart');
	 
	$server->wsdl->schemaTargetNamespace = 'urn:opencart';
	 	 
	// register a function with name "whatsNew"
	$server->register('whatsNew',
				array('json' => 'xsd:string'),  //parameter
				array('return' => 'xsd:string'),  //output
				'urn:server',   //namespace
				'urn:server#opencart',  //soapaction
				'rpc', // style
				'encoded', // use
				'Fetch latest tech');  //description
	 
	//function implementation
	function whatsNew($json) {

		$data = json_decode($json);

		$username = $data->username;

		$latest = array('Google Glass','Form 1','Oculus Rift','Leap Motion','Eye Tribe','SmartThings','Firefox OS','Project Fiona','Parallella','Google Driverless Car');
		$string = '';

		foreach ($latest as $key) {
			$string .= $key.', ';
		}
		$latest = rtrim($string,', ');
	    return 'Hello, '.$username.', latest technologies of 2016 are '.$latest.'.';
	}
		 
	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	 
	$server->service($HTTP_RAW_POST_DATA);
?>

3. Creating Client

Now, it’s time for you to create a client file that will hit the API. Use the following code to fetch the information from the server.

<?php
	require_once('../lib/nusoap.php');
	 
	//This is your webservice server WSDL URL address
	$wsdl = "http://192.168.1.29/server.php?wsdl";
	 
	//create client object
	$client = new nusoap_client($wsdl, 'wsdl');

	$data = array(
			'username'=>'John Doe'
		);

	$json = json_encode($data);
        
        // passing function name "whatsNew" and json encoded data
	$result = $client->call('whatsNew', array('json'=>$json));

	if ($result) {
		print_r($result);
	} else {
		print_r('Error!!!');
	}
	
?>

4. Output

The output at the client’s end would be like:

Hello, John Doe, latest technologies of 2016 are Google Glass, Form 1, Oculus Rift, Leap Motion, Eye Tribe, SmartThings, Firefox OS, Project Fiona, Parallella, Google Driverless Car.

 


Viewing all articles
Browse latest Browse all 5552

Trending Articles