Get the Top Affiliates report via API

The following sample script shows how to get the Top Affiliates report data via API.
 
<?php
// the PapApi.class.php file can be downloaded from the merchant panel at:
// Tools > Integration > API Integration > Download PAP API

// This include assumes the PapApi.class.php file is in the same directory as this particular script. 
include 'PapApi.class.php'; 

$papURL = "http://URL_TO_PostAffiliatePro"; //URL to Post Affiliate Pro without any trailing slash
$merchantUsername = "merchant@username.com"; //merchant username
$merchantPassword = "123456";

//----------------------------------------------
// login (as merchant)
// change localhost/pap to the real URL leading to your installation of Post Affiliate Pro
$session = new Pap_Api_Session($papURL."/scripts/server.php"); 
//$session->setDebug();
if(!@$session->login($merchantUsername, $merchantPassword)) {
  exit("Cannot login. Message: ".$session->getMessage());
}

//----------------------------------------------
// get the list of Top Affiliates records
$request = new Gpf_Rpc_GridRequest("Pap_Merchants_User_TopAffiliatesGrid","getRows",$session);

//add support of additional columns for the php API
$request->addParam('columns', new Gpf_Rpc_Array(array(array('id'), array('userid'), 
array('username'), array('firstname'), array('lastname'), 
array('clicksRaw'), array('salesCount'))));

//to see all available parameters, check the following article:
// http://support.qualityunit.com/775501-Debugging-with-Firefox

$request->setLimit(0, 30);
//filtering stats only for the last (previous) month
$request->addFilter("statsdaterange",Gpf_Data_Filter::DATERANGE_IS,Gpf_Data_Filter::RANGE_LAST_MONTH);
//more info on date/time range options: http://support.qualityunit.com/233666-Gpf_Rpc_GridRequest

//sorting based on sales count
$request->setSorting('salesCount', false); //true = Ascending; false = Descending

// send request
try {
  $request->sendNow();
} catch(Exception $e) {
  exit("API call error: ".$e->getMessage());
}

// request was successful, get the grid result
$grid = $request->getGrid();

// get recordset from the grid
$recordset = $grid->getRecordset();
//defining the html table and its headers

// iterate through the records
foreach($recordset as $rec) {	
	echo 'Affiliate: '.$rec->get('firstname').' '.$rec->get('lastname').' ('.$rec->get('username').'), Raw clicks: '.$rec->get('clicksRaw').', Sales Count: '.$rec->get('salesCount')."<br>";
}

//----------------------------------------------
// in case there are more than 30 records in total,
// we should load and display the rest of the records
// via the cycle below

$totalRecords = $grid->getTotalCount();
$maxRecords = $recordset->getSize();

if ($maxRecords != 0) {
  $cycles = ceil($totalRecords / $maxRecords);
  for($i=1; $i<$cycles; $i++) {
    // now get next 30 records
    $request->setLimit($i * $maxRecords, $maxRecords);
    $request->sendNow();
    $recordset = $request->getGrid()->getRecordset();
    // iterate through the records
    foreach($recordset as $rec) {
      echo 'Affiliate: '.$rec->get('firstname').' '.$rec->get('lastname').' ('.$rec->get('username').'), Raw clicks: '.$rec->get('clicksRaw').', Sales Count: '.$rec->get('salesCount')."<br>";
    }
  }
}
?>