NATS4 API caching
From TMM Wiki
Jump to navigationJump to searchWSDL Caching
Nusoap has built-in WSDL caching using the nusoap_wsdlcache class. This will allow you to store the WSDL locally in a cache file, instead of requesting it each time, reducing the amount of requests needed.
Example
Here is an example of using caching, building off the example from the Main API article
<? require_once('nusoap/lib/nusoap.php'); require_once('nusoap/lib/class.wsdlcache.php'); $url = 'http://nats.site.com/admin_api.php'; // change to be the domain of your NATS install $url.='?wsdl'; $username = 'NATSADMIN'; // your admin username $apikey = '3385e1b470b0864e27a9b648da6c0692'; // your api key $cache = new nusoap_wsdlcache('/tmp', 86400); // Check to see if we have a cache, if so use it $wsdl = $cache->get($url); $needcache = false; if(is_null($wsdl)){ $needcache = true; }else{ $url=$wsdl; } $client = new nusoap_client($url, true); $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 } //If we need the cache, and we got the WSDL, add the cache if($needcache && !empty($client->wsdl) && !empty($client->wsdl->schemas)){ $cache->put($client->wsdl); }
To give a complete example of this, here's an example using the ping function. This is how to call it:
<? require_once('nusoap/lib/nusoap.php'); require_once('nusoap/lib/class.wsdlcache.php'); $url = 'http://nats.site.com/admin_api.php'; // change to be the domain of your NATS install $url.='?wsdl'; $username = 'NATSADMIN'; // your admin username $apikey = '3385e1b470b0864e27a9b648da6c0692'; // your api key $cache = new nusoap_wsdlcache('/tmp', 86400); // Check to see if we have a cache, if so use it $wsdl = $cache->get($url); $needcache = false; if(is_null($wsdl)){ $needcache = true; }else{ $url=$wsdl; } $client = new nusoap_client($url, true); $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 } $result = $client->call('ping', Array(), 'natsapiadmin_wsdl'); var_dump($result); //If we need the cache, and we got the WSDL, add the cache if($needcache && !empty($client->wsdl) && !empty($client->wsdl->schemas)){ $cache->put($client->wsdl); }
And this is the output:
bool(true)
Multiple Requests
If you are making multiple requests to the same API in a script, keep in mind that you do not need to recreate the nusoap client with each request -- just do a new call. Here is an example:
<? require_once('nusoap/lib/nusoap.php'); require_once('nusoap/lib/class.wsdlcache.php'); $url = 'http://nats.site.com/admin_api.php'; // change to be the domain of your NATS install $url.='?wsdl'; $username = 'NATSADMIN'; // your admin username $apikey = '3385e1b470b0864e27a9b648da6c0692'; // your api key $cache = new nusoap_wsdlcache('/tmp', 86400); // Check to see if we have a cache, if so use it $wsdl = $cache->get($url); $needcache = false; if(is_null($wsdl)){ $needcache = true; }else{ $url=$wsdl; } $client = new nusoap_client($url, true); $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 } // you can do the requests sequentially $result = $client->call('ping', Array(), 'natsapiadmin_wsdl'); var_dump($result); $result = $client->call('ping', Array(), 'natsapiadmin_wsdl'); var_dump($result); $result = $client->call('ping', Array(), 'natsapiadmin_wsdl'); var_dump($result); //or in a loop for($i=0; $i < 5; $i++){ $result = $client->call('ping', Array(), 'natsapiadmin_wsdl'); var_dump($result); } //If we need the cache, and we got the WSDL, add the cache if($needcache && !empty($client->wsdl) && !empty($client->wsdl->schemas)){ $cache->put($client->wsdl); }