Get (retrieve) banner codes via API (only for affiliates)

If some affiliates wish to obtain their banner codes via API so that they add all their banners to their page(s) dynamically and the fastest way possible, then the following php API example will come in handy.

Note: The first parameter of setLimit() is an offset that is no longer used since version 5.11.8.1 — always pass 0. Paging is handled automatically: sending the same request object again returns the next page of records. The maximum number of records returned in one request is 500. Use the largest page size your script can handle, as it means fewer API calls. Details are described in the Gpf_Rpc_GridRequest article.

<?php
define('URL_TO_PAP','https://URL_To_PostAffiliatePro'); //URL to Post Affiliate Pro (network) without any trailing slash
define('AFFILIATE_USERNAME','affiliate@example.com'); //affiliate username
define('AFFILIATE_PASSWORD','demo'); //affiliate password
define('API_FILE','PapApi.class.php');
//the PapApi.class.php file can be downloaded in the merchant panel: Tools>Integration>API Integration>Download PAP API
//make sure the PapApi.class.php file is uploaded in the same directory as this script itself.

$session = papAffiliateLogin();
if (!$session){
    die('Banner codes cannot be obtained due to failed login attempt');
}

$bannerCodes = getBannerCodes($session); //obtaining the banner codes as a genric HTML code that is directly executed
$bannerCodesNeatCode = getBannerCodes($session,'Y'); //utlizing https://php.net/htmlspecialchars
if ($bannerCodes) {
    foreach ($bannerCodes as $code){
        echo $code."<hr>";
    }
}
if ($bannerCodesNeatCode) {
    foreach ($bannerCodesNeatCode as $code){
        echo $code."<hr>";
    }
}

/*definition of functions to be used*/
function papAffiliateLogin(){
    try {
        include_once API_FILE;
        $session = new Pap_Api_Session(URL_TO_PAP."/scripts/server.php");
        if(!$session->login(AFFILIATE_USERNAME, AFFILIATE_PASSWORD, Gpf_Api_Session::AFFILIATE)) { //change it here for affiliate username and password
            die("Cannot login. Message: ".$session->getMessage());
        }
        return $session;
    } catch (Exception $e){
        die('Error: '.$e->getMessage());
    }
}

function getBannerCodes($session,$htmlSpecialChars=NULL){
    $bannerCodes = array();
    //----------------------------------------------
    // get recordset with list of banners
    $request = new Gpf_Rpc_Gridrequest("Pap_Affiliates_Promo_BannersGrid","getRows",$session);
    // sets the page size to 500 rows, which is the maximum per request
    // the first parameter is an offset which is not used, always keep it at 0
    $request->setLimit(0, 500);
    // Filter particular banner type(s); A = link banner, E = promo email, F = flash, H = HTML, I = image, V = Simple PDF, T = Text-Link
    //$request->addFilter('rtype',Gpf_Data_Filter::EQUALS,'E');
    //$request->addFilter('id',Gpf_Data_Filter::EQUALS,'11110001');  // use this for filtering exact banner ID

    // execute request in a loop to load all data
    // sending the same request again returns the next page
    do {
        // send request
        try {
            $request->sendNow();
        } catch(Exception $e) {
            die("API call error: ".$e->getMessage());
        }
        // request was successful, get the grid result and its recordset
        $recordset = $request->getGrid()->getRecordset();
        // iterate through the records and fill in the results array
        foreach($recordset as $rec) {
            if(isset($htmlSpecialChars) && $htmlSpecialChars == 'Y'){
                $bannerCodes[] = htmlspecialchars($rec->get('bannercode'));
            } else {
                $bannerCodes[] = $rec->get('bannercode');
            }
        }
    } while ($recordset->getSize() == $request->getLimit());
    return $bannerCodes;
}
?>