Add channel via API

Sample code to add channel via API.
NOTE: It requires affiliate login credentials
 
<?php
$desiredChannelName = "MyTestChannel"; //up to 255 characters
$desiredChannelCode = "MTC"; //up to 255 characters

$papURL = "http://localhost/pap"; //URL to PAP without trailing slash
$affiliateUsername = "affiliate@username.com";
$affiliatePassword = "password";

//the PapApi.class.php can be found at the PAP installation folder, in the 'api' directory
//or downloaded from the merchant panel
include 'PapApi.class.php'; 

$session = new Pap_Api_Session($papURL."/scripts/server.php");
if(!$session->login($affiliateUsername, $affiliatePassword, Pap_Api_Session::AFFILIATE)) {	
	die("Login failed: ".$session->getMessage());
}


//check if channel with the desired channel code already exists or not
$request0 = new Gpf_Rpc_GridRequest("Pap_Affiliates_Promo_ChannelsGrid","getRows",$session);
try {
  $request0->sendNow();
} catch(Exception $e) {
  die("API call error: ".$e->getMessage());
}

$grid = $request0->getGrid();
$recordset = $grid->getRecordset();

foreach($recordset as $rec) {
	if ($rec->get('channel') == $desiredChannelCode) {
		die ('Channel with the entered channel code already exists');
	}
}


//Create default channel field/row
$request1 = new Gpf_Rpc_GridRequest("Pap_Affiliates_Promo_ChannelsGrid","getRowsAddNew",$session);
$request1->setSorting('channel');

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

$grid = $request1->getGrid();
$recordset = $grid->getRecordset();

foreach($recordset as $rec) {
	if (substr($rec->get('channel'), 0, 4) == 'code') {
		updateNewChannel($rec->get('id'), $desiredChannelName, $desiredChannelCode, $session);
		break;
	}
}


function updateNewChannel($id, $name, $code, $session) {
	$request = new Gpf_Rpc_Request("Pap_Affiliates_Promo_ChannelsForm", "saveFields", $session);

	$recordset = new Gpf_Data_RecordSet();

	$recordset->setHeader(array('id', 'name', 'value'));
	$recordset->add(array($id, 'name', $name));
	$recordset->add(array($id,'channel',$code));

	$request->addParam('fields', $recordset);

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

?>