NATS4 API caching

From TMM Wiki
Jump to navigationJump to search
NATS 4
Members Admin
The Members Admin
View Member Details
Add Member
MySQL Auth
Mod Authn DB
Multisite Access
Member Logging
Member Password Retrieval
OpenID Connect
Mod Auth OpenIDC
ID Numbers
SOAP API
API
API Best Practices
WSDL Cache
Add Affiliate
Add Member Note
Admin Get Adtools
Adtool Categories
Adtool Types
Affiliate Get Campaigns
Bulk Import Adtools
Caching
Decode Natscode
Expire Manual Member
Get Affiliate Campaigns
Get Affiliate Hit Data
Get Affiliate Loginids
Get Affiliate Nats Codes
Get Affiliate Payout
Get Affiliate Program Campaign List
Get_Affiliate_Program_Campaign_List
Get Member Details
Get Member Instant Upgrade String
Get Member Package Upgrade String
Get Member Token Rebuy String
Get Member Upsell String
Get Payment Data
Get Payvia Rule
Get Profit Loss Report
Ping
Record Member Login
Search Affiliate Info
Search Member Info
Send Email API Function
Set Affiliate Admin Settings
Set Affiliate Customs
Set Affiliate Defaults
Set Affiliate Information
Set Affiliate Settings
Set Member Details
Set Payment Status
Set Payvia Rule
REST API
API Overview
API Best Practices
REST API PATH UPDATES
Adtools
GET /adtools/admin
GET /adtools/categories
GET /adtools/types
POST /adtools/importdump
Affiliate
GET /affiliate/campaigns
GET /affiliate/hitdata
GET /affiliate/payout
GET /affiliate/searchinfo
POST /affiliate/addaffiliate
POST /affiliate/invoice
PATCH /affiliate/setadminsettings
PATCH /affiliate/setcustoms
PATCH /affiliate/setdefaults
PATCH /affiliate/setinformation
PATCH /affiliate/setpayviainfo
PATCH /affiliate/setsettings
PATCH /affiliate/status
Member
GET /member/authstring
GET /member/details
GET /member/searchinfo
GET /suggestedcanceloffers
PATCH /member/setdetails
PATCH /member/setexpiration
POST /member/addnote
POST /member/recordlogin
PUT /member/expiremanual
PATCH /member/forget
Option
GET /option/options
GET /option/rule
PATCH /option/rule
PATCH/option/text
POST /option/rule
Payments
GET /payments/getpayments
GET /payviarule
PATCH /payments/setstatus
PATCH /payviarule
Report
GET /profitlossreport
Get /transactionpayouts
GET /report/transaction
Service
GET /service/decodenatscode
GET /service/ping
POST /service/sendemail

WSDL 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);
}