searchCruises(['region' => 'caribbean', 'limit' => 3]); * echo $found['total'], "\n"; * } catch (TratokLabsError $e) { * echo $e->getErrorCode(), ': ', $e->getMessage(), ' (', $e->getRequestId(), ")\n"; * } * * Every method takes one array of fields and returns the answer of the API as an array. A refused call throws * a TratokLabsError. Field names are those of the reference: https://developer.tratok.net/docs.php * * You may use, copy and change this file in your own projects. It comes without warranty of any kind. */ /** A call that was refused, or that never got an answer. */ class TratokLabsError extends \Exception { private $errorCode; private $status; private $requestId; private $retryAfter; private $body; public function __construct($message, array $info = []) { parent::__construct((string) $message); $this->errorCode = isset($info['code']) && $info['code'] !== '' ? (string) $info['code'] : 'unknown'; $this->status = isset($info['status']) ? (int) $info['status'] : 0; $this->requestId = isset($info['requestId']) ? $info['requestId'] : null; $this->retryAfter = isset($info['retryAfter']) ? $info['retryAfter'] : null; $this->body = isset($info['body']) ? $info['body'] : null; } /** The short code of the API, such as invalid_credentials; 'network' or 'timeout' when no answer came. */ public function getErrorCode() { return $this->errorCode; } /** The HTTP status; 0 when no answer came. */ public function getStatus() { return $this->status; } /** Quote this if you write to us. */ public function getRequestId() { return $this->requestId; } /** Seconds to wait, with rate_limited. */ public function getRetryAfter() { return $this->retryAfter; } /** The whole answer, when there was one. */ public function getBody() { return $this->body; } } class TratokLabs { const VERSION = '1.0.0'; /** Facts about the last answer: status, requestId, rateLimitLimit, rateLimitRemaining, rateLimitReset, freeCallsRemaining. */ public $lastMeta = null; private $appId; private $secret; private $baseUrl; private $timeout; /** * @param string|int $appId your app ID * @param string $secret your secret (or a test key, for the booking test mode) * @param array $options baseUrl, timeout (seconds) */ public function __construct($appId, $secret, array $options = []) { if ((string) $appId === '' || (string) $secret === '') { throw new \InvalidArgumentException('TratokLabs needs an app ID and a secret'); } if (!function_exists('curl_init')) { throw new \RuntimeException('TratokLabs needs the curl extension of PHP'); } $this->appId = (string) $appId; $this->secret = (string) $secret; $this->baseUrl = isset($options['baseUrl']) ? (string) $options['baseUrl'] : 'https://developer.tratok.net/v1/'; $this->timeout = isset($options['timeout']) ? (int) $options['timeout'] : 30; } /** Call any method by its name. Fields that are null are not sent. */ public function call($method, array $fields = []) { $body = ['request' => (string) $method, 'id' => $this->appId, 'secret' => $this->secret]; foreach ($fields as $key => $value) { if ($value !== null) { $body[$key] = $value; } } $headers = []; $ch = curl_init($this->baseUrl); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($body), CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => 'tratok-labs-php/' . self::VERSION, CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Accept: application/json'], CURLOPT_HEADERFUNCTION => function ($handle, $line) use (&$headers) { $parts = explode(':', $line, 2); if (count($parts) === 2) { $headers[strtolower(trim($parts[0]))] = trim($parts[1]); } return strlen($line); }, ]); $raw = curl_exec($ch); $errno = curl_errno($ch); $error = curl_error($ch); $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); unset($ch); if ($raw === false) { $timedOut = ($errno === 28); throw new TratokLabsError($timedOut ? 'The call timed out' : 'The call could not be made: ' . $error, ['code' => $timedOut ? 'timeout' : 'network']); } $number = function ($name) use ($headers) { return isset($headers[$name]) && is_numeric($headers[$name]) ? (int) $headers[$name] : null; }; $this->lastMeta = [ 'status' => $status, 'requestId' => isset($headers['x-request-id']) ? $headers['x-request-id'] : null, 'rateLimitLimit' => $number('x-ratelimit-limit'), 'rateLimitRemaining' => $number('x-ratelimit-remaining'), 'rateLimitReset' => $number('x-ratelimit-reset'), 'freeCallsRemaining' => $number('x-free-calls-remaining'), ]; $data = json_decode((string) $raw, true); if (!is_array($data)) { throw new TratokLabsError('The answer was not JSON (HTTP ' . $status . ')', ['code' => 'bad_answer', 'status' => $status, 'requestId' => $this->lastMeta['requestId'], 'body' => $raw]); } if ($status >= 400 || (isset($data['status']) && $data['status'] === 'Error')) { throw new TratokLabsError(isset($data['message']) ? $data['message'] : 'The call was refused', [ 'code' => isset($data['code']) ? $data['code'] : 'unknown', 'status' => $status, 'requestId' => isset($data['requestId']) ? $data['requestId'] : $this->lastMeta['requestId'], 'retryAfter' => isset($data['retryAfter']) ? $data['retryAfter'] : null, 'body' => $data, ]); } return $data; } // Hotels /** Fields: country. */ public function hotelsInCountry(array $fields = []) { return $this->call('hotelsInCountry', $fields); } /** Fields: city. */ public function hotelsInCity(array $fields = []) { return $this->call('hotelsInCity', $fields); } /** Fields: hotelId. */ public function getHotelInformation(array $fields = []) { return $this->call('getHotelInformation', $fields); } /** Fields: hotelId. */ public function getHotelGallery(array $fields = []) { return $this->call('getHotelGallery', $fields); } // Car rentals /** No fields. */ public function getListOfCarCities(array $fields = []) { return $this->call('getListOfCarCities', $fields); } /** Fields: city. */ public function getListOfAllCarVenuesInCity(array $fields = []) { return $this->call('getListOfAllCarVenuesInCity', $fields); } /** Fields: locationId. */ public function getListOfCarsAtVenue(array $fields = []) { return $this->call('getListOfCarsAtVenue', $fields); } // Cruises /** Fields: q?, region?, lineId?, port?, month?, nightsMin?, nightsMax?, priceMax?, sort?, limit?, offset?. (? = optional) */ public function searchCruises(array $fields = []) { return $this->call('searchCruises', $fields); } /** Fields: cruiseId. */ public function getCruise(array $fields = []) { return $this->call('getCruise', $fields); } /** No fields. */ public function getCruiseRegions(array $fields = []) { return $this->call('getCruiseRegions', $fields); } /** No fields. */ public function getCruiseLines(array $fields = []) { return $this->call('getCruiseLines', $fields); } // Translation /** No fields. */ public function supportedLanguages(array $fields = []) { return $this->call('supportedLanguages', $fields); } /** Fields: text. */ public function detectLanguage(array $fields = []) { return $this->call('detectLanguage', $fields); } /** Fields: text, sourceLanguage?, targetLanguage. (? = optional) */ public function translate(array $fields = []) { return $this->call('translate', $fields); } // Tara AI /** Fields: message, conversationId?, maxTokens?. (? = optional) */ public function taraChat(array $fields = []) { return $this->call('taraChat', $fields); } /** Fields: conversationId. */ public function taraReset(array $fields = []) { return $this->call('taraReset', $fields); } // Inventory and booking /** Fields: q?, city?, country?, nearLat?, nearLng?, radiusKm?, limit?, offset?. (? = optional) */ public function searchStays(array $fields = []) { return $this->call('searchStays', $fields); } /** Fields: q?, city?, country?, nearLat?, nearLng?, radiusKm?, limit?, offset?. (? = optional) */ public function searchActivities(array $fields = []) { return $this->call('searchActivities', $fields); } /** Fields: q?, city?, country?, nearLat?, nearLng?, radiusKm?, limit?, offset?. (? = optional) */ public function searchRestaurants(array $fields = []) { return $this->call('searchRestaurants', $fields); } /** Fields: vertical?, listingId. (? = optional) */ public function getListing(array $fields = []) { return $this->call('getListing', $fields); } /** Fields: accessToken, vertical, listingId, roomId?, packageId?, checkIn?, checkOut?, guests?, date?, participants?, time?, party?. (? = optional) */ public function getQuote(array $fields = []) { return $this->call('getQuote', $fields); } /** Fields: accessToken, vertical?, listingId?, roomId?, packageId?, checkIn?, checkOut?, guests?, date?, participants?, time?, party?, quoteId, idempotencyKey, guestName?, guestPhone?, notes?. (? = optional) */ public function bookListing(array $fields = []) { return $this->call('bookListing', $fields); } /** Fields: accessToken, tab?. (? = optional) */ public function myBookings(array $fields = []) { return $this->call('myBookings', $fields); } /** Fields: accessToken, bookingId. */ public function getBooking(array $fields = []) { return $this->call('getBooking', $fields); } /** Fields: accessToken, bookingId. */ public function cancelBooking(array $fields = []) { return $this->call('cancelBooking', $fields); } }