/** * Resets the session, if access token exists, tries to use it * * @return bool */ public function reset() { if (!$this->AccessToken && !empty($_SESSION[self::ODR_TOKEN_SESSION])) { $this->AccessToken = $_SESSION[self::ODR_TOKEN_SESSION]; } if (strpos($this->User, 'public$') !== 0) { return $this->parseError('Vul de API key als gebruikersnaam in en de API secret als wachtwoord.'); } // @codeCoverageIgnoreStart if (!$this->odr) { $config = array('api_key' => $this->User, 'api_secret' => $this->Password, 'url' => $this->Testmode ? self::URL_TEST : self::URL_LIVE); $this->odr = new Api_Odr($config); } // @codeCoverageIgnoreEnd if ($this->AccessToken && is_string($this->AccessToken)) { try { $this->odr->setHeader('X-Access-Token', $this->AccessToken); $meResult = $this->odr->getMe()->getResult(); if ($meResult['status'] === Api_Odr::STATUS_ERROR) { if (!empty($_SESSION[self::ODR_TOKEN_SESSION])) { unset($_SESSION[self::ODR_TOKEN_SESSION]); } $this->AccessToken = false; $this->odr->setHeader('X-Access-Token', null); } } catch (Api_Odr_Exception $e) { if (!empty($_SESSION[self::ODR_TOKEN_SESSION])) { unset($_SESSION[self::ODR_TOKEN_SESSION]); } $this->AccessToken = false; $this->odr->setHeader('X-Access-Token', null); } if ($this->AccessToken) { return true; } } try { $loginResult = $this->odr->login()->getResult(); if ($loginResult['status'] === Api_Odr::STATUS_ERROR) { return $this->parseError($loginResult['response'], $loginResult['code']); } $this->AccessToken = $loginResult['response']['token']; $_SESSION[self::ODR_TOKEN_SESSION] = $this->AccessToken; } catch (Api_Odr_Exception $e) { $this->Error[] = $e->getMessage(); return false; } return true; }
<?php // Require ODR API demo class require_once '../Api/Odr.php'; // Configuration array, with user API Keys $config = array('api_key' => '#API_KEY#', 'api_secret' => '#API_SECRET#'); // We assume that user already sent all the data to us through request $data = $_REQUEST; // Create new instance of API demo class $demo = new Api_Odr($config); // Login into API $demo->login(); $loginResult = $demo->getResult(); if ($loginResult['status'] === 'error') { echo 'Can\'t login, reason - ' . $loginResult['response']; exit(1); } // Create new contact, by passing request data $demo->createContact($data); // Get result of request $result = $demo->getResult(); if ($result['status'] !== 'success') { echo 'Following error occured: ' . $result['response']; exit(1); } $result = $result['response']; // Contact successfully created, yay! echo 'Contact "' . $result['contact_first_name'] . ' ' . $result['contact_last_name'] . '" created';