class representing session (logged user). You must log in using this object before making any other requests.
Methods:
constructor Pap_Api_Session($url) – creates session object. You have to specify full valid URL to the /scripts/server.php file of your installation.
For example: https://www.yoursite.com/affiliate/scripts/server.php
login($username, $password, $roleType, $languageCode) – authorizes the user and creates session object. The username and password must be your merchant or affiliate login information.
By default the user is considered to be a merchant. If you want to log in as an affiliate, you have to specify the third parameter $roleType = self::AFFILIATE.
$languageCode parameter is optional. If you do not specify any value for this parameter, default language will be used. Language can be secified by its code e.g. en-US, de-DE, sk, cz, ...
If login was successful, it returns true, otherwise false. If you use incompatible API version, login throws Gpf_Api_IncompatibleVersionException (see bellow for more details).
setDebug(true/false) – sets debugging. If you set it to true, it will output every request and response in JSON format.
getMessage() - returns error message in case of error
Logging in as merchant:
$session = new Pap_Api_Session('https://demo.postaffiliatepro.com/scripts/server.php'); if(!$session->login('merchant@example.com', 'demo')) { die("Cannot login. Message: ".$session->getMessage()); }
Logging in as affiliate
$session = new Pap_Api_Session('https://demo.postaffiliatepro.com/scripts/server.php'); if(!$session->login('affiliate@example.com', 'demo', Pap_Api_Session::AFFILIATE)) { die("Cannot login. Message: ".$session->getMessage()); }
Of course, as an affiliate you don't have access to the functionality of merchant, for example to the list of all affiliates.
If you want to redirect user (merchant/affiliate) to his panel after Pap_Api_Session is created you can do it using following code:
// redirecting to merchant panel (requires merchant session) header('Location: '.$session->getUrlWithSessionInfo('https://demo.postaffiliatepro.com/merchants/index.php')); // redirecting to affiliate panel (requires affiliate session) header('Location: '.$session->getUrlWithSessionInfo('https://demo.postaffiliatepro.com/affiliates/panel.php'));
<?php include_once('PapApi.class.php'); $session = new Pap_Api_Session('https://www.example.com/affiliate/scripts/server.php'); if ($papversion = $session->getAppVersion()) { echo 'Post Affiliate Pro version: ' . $papversion; } else { echo 'Failed to load version with error: ' . $session->getMessage(); } ?>
$session->login(...) throws Gpf_Api_IncompatibleVersionException exception in case when your PapApi.class.php file is not compatible with PostAffiliatePro you are connecting to. Example of handling this exception:
$session = new Pap_Api_Session('https://demo.postaffiliatepro.com/scripts/server.php'); try { if (!$session->login('merchant@example.com', 'demo')) { die("Unable to login: ".$session->getMessage()); } echo "logged"; } catch (Gpf_Api_IncompatibleVersionException $e) { echo "Download new API file here: ".$e->getApiDownloadLink(); }
Method getApiDownloadLink() in Gpf_Api_IncompatibleVersionException returns full url o the PapApi.class.php file of the installation you are connecting to so you can download it and update your PapApi.class.php file.
setSessionId($sessionId) - use this instead of login() function, for login from session id. If you wish to log in as affiliate, also specify the role with second parameter set to Pap_Api_Session::AFFILIATE.
Authenticate as merchant:
$session = new Pap_Api_Session('https://demo.postaffiliatepro.com/scripts/server.php'); $session->setSessionId('wnvzljj8a3fdo3sjrplm7oqdjimcb7n8');
Authenticate as affiliate:
$session = new Pap_Api_Session('https://demo.postaffiliatepro.com/scripts/server.php'); $session->setSessionId('sdf4sdfg5465sdfgqw65uytkm68fguku', Pap_Api_Session::AFFILIATE);