Get affiliate ID based on the visitor ID

Many times you will need to use affiliate ID but you only have visitor ID value from cookies. Use this example to help you to get the affiliate ID (or any other value) based on the visitor ID.
include 'PapApi.class.php'; //Download PapApi.class.php from the merchant panel

$session = new Pap_Api_Session('');

if(!@$session->login("","merchantpassword")) {
  die("Cannot login. Message: ". $session->getMessage());

$visitorId = 'default1a5db4c5158fe6a872c1ecac09176520'; //you should obtain the PAPVistorId value dynamically, e.g. via $_COOKIE or $_GET or $_POST parameter

if (strlen($visitorId) > 32) {
	$visitorId = substr($visitorId, -32);	

$request = new Gpf_Rpc_GridRequest('Pap_Merchants_Tools_VisitorAffiliatesGrid', 'getRows', $session);
$request->addParam('columns', new Gpf_Rpc_Array(array(array('id'), array('visitoraffiliateid'),
array('visitorid'), array('userid'), array('username'), array('firstname'), array('lastname'),
array('banner'), array('bannerid'), array('banner_name'), array('banner_type'), array('banner_id'),
array('channelid'), array('campaignid'), array('isconfirmed'), array('name'), array('rtype'), array('ip'),
array('datevisit'), array('validto'), array('referrerurl'), array('data1'), array('data2'), array('accountid'))));
// set filter
$request->addFilter("visitorid", Gpf_Data_Filter::EQUALS, $visitorId);
$request->addFilter("rtype", Gpf_Data_Filter::EQUALS, 'A');  //remove this line if you are using 'Split Commissions' feature
$request->addFilter("validto", Gpf_Data_Filter::DATE_EQUALS_GREATER, date('Y-m-d'));
//in PAN insert here your merchant network accountid
//$request->addFilter("accountid", Gpf_Data_Filter::EQUALS, 'default1');
$request->setLimit(0, 1);

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

$grid = $request->getGrid();

$recordset = $grid->getRecordset();

if ($recordset->getSize() > 0) {
  echo $recordset->get(0)->get('userid');

Additional parameters that you can use instead of 'userid':
visitorid,userid,username,firstname,lastname,bannerid,banner_name,banner_type,banner_id,channelid,campaignid,name, rtype,ip,datevisit,validto,referrerurl,data1,data2,accountid

name = campaign name; data1,data2 = click data1,2;
Another possible solution is to use the "writeAffiliateToCustomField('affCookieInfoId')" function. You can view its usage at  --- view the source code of that page via your browser (CTRL+U)