Get the list of affiliates invited to any private campaign

This sample script shows the same result as the "Campaigns > Affiliates in campaigns" grid in the merchant panel.
More exactly, it lists the affiliates and names of private (public with manual approval) campaigns the affiliates belong to.
 
<?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 PAP/PAN without trailing slash

$session = new Pap_Api_Session($papURL."/scripts/server.php"); 
if(!@$session->login("merchant@user.com", "kindOfPassword")) {
  die("Cannot login. Message: ".$session->getMessage());
}

$request = new Gpf_Rpc_GridRequest("Pap_Features_Common_AffiliateCampaignsGrid", "getRows", $session);
//sets limit to 30 rows, offset to 0 (start with first row)
$request->setLimit(0, 30);

//set sorting by campaign name
$request->setSorting("campaignname", "sort_asc"); 

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

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

// get recordset from the grid
$recordset = $grid->getRecordset();

// iterate through the records
foreach($recordset as $rec) {
	echo 'Campaign Name: '.$rec->get('campaignname').' Affiliate name: '.$rec->get('firstname').' '.$rec->get('lastname').'<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 'Campaign Name: '.$rec->get('campaignname').' Affiliate name: '.$rec->get('firstname').' '.$rec->get('lastname').'<br>';
		}	  
	}
}
?>

 
Available parameters:
campaignname,userid,username,firstname,lastname,rstatus,dateadded,note,actions