NATS4 API nusoap wsdlcache

From TMM Wiki
Revision as of 23:45, 2 February 2016 by TMMNick (talk | contribs) (Created page with "When using the SOAP API, you can improve performance by caching the NATS wsdl file. This article shows an example of using the "nusoap_wsdlcache" to do so. == nusoap_wsdlcac...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

When using the SOAP API, you can improve performance by caching the NATS wsdl file. This article shows an example of using the "nusoap_wsdlcache" to do so.

nusoap_wsdlcache example

To use the "nusoap_wsdlcache" class, you will first need to include it at the top of your script making the API calls:

/* nusoap includes */
require_once('nusoap/lib/nusoap.php');
if(!class_exists("nusoap_wsdlcache")){ 
	require_once('nusoap/lib/class.wsdlcache.php');
}

The "nusoap_wsdlcache" takes 2 parameters when declaring a new instance:

  • Directory where cache files will be stored (<cache_dir> in example below)
  • The time limit of the cache file in seconds (<wsdl_cache_lifetime> in example below)
//API login vars
$username = '<admin_username>'; // your admin username
$apikey = '<api_key>'; // your api key
$url = 'http://<your_nats_domain>/admin_api.php?wsdl'; // change <your_nats_domain>  to be domain of your NATS install

/* Nusoap WSDL cache setup */
//cache vars
$cache_dir = "<cache_dir>"; // change <cache_dir> to be the directory for storing cache files
$wsdl_cache_lifetime = <wsdl_cache_lifetime>; // change  <wsdl_cache_lifetime> to an amount in seconds

//construct cache
$wsdlCache = new nusoap_wsdlcache($cache_dir, $wsdl_cache_lifetime);

//try to get cache
$wsdl = $wsdlCache->get($url);
if (is_null($wsdl)) {
	//cache file not found, need to create new one
	$wsdl = new wsdl();
	$wsdl->setCredentials($username,$apikey);// need to login for NATS wsdl 
	$wsdl->fetchWSDL($url); //get the wsdl
	$wsdlCache->put($wsdl); //create cache file
}
$client = new nusoap_client($wsdl, TRUE);
/* end Nusoap WSDL cache setup */

//set your login for the soap client
$client->setCredentials($username,$apikey);

// Check for an error
$err = $client->getError();
if ($err) {
    // Display the error
    echo 'Constructor error' . $err . "\n";
    exit; // At this point, you know the call that follows will fail
    
}

//make an API call
$result = $client->call('ping', Array(), 'natsapiadmin_wsdl');
var_dump($result);