* LocationOf.com * March 29th 2011 (Modified June 4th 2017) * * ==== * Documentation can be found at http://www.locationof.com/api/ * ==== * * PHP Dependencies: * - cURL ( http://php.net/manual/en/book.curl.php ) * * This file does not need to be modified for usage. */ class LocationOf { private $apiKey; private $useCache = true; private static $cacheFolder = 'LocationOf_cache'; function __construct($apiKey = 'demo') { $this->apiKey = $apiKey; } public function setUseCache($b) { $this->useCache = is_bool($b) ? $b : true; } public function get($timestampStart = 0, $timestampEnd = 0) { if (!$this->useCache || @filemtime(self::getCacheFolderPath() . ($cacheFile = $this->apiKey . '_' . $timestampStart . '_' . $timestampEnd . '.cache')) <= time() - 15) { /* Test for cURL extension */ if (!function_exists('curl_init')) self::displayError("Your version of PHP does not have cURL support.\nMake sure to compile and/or enable the PHP cURL Extension."); /* Start cURL request */ $curl = curl_init(); $data = array( 'key' => $this->apiKey, 'start' => $timestampStart, 'end' => $timestampEnd, 'av' => 1 ); if (isset($_SERVER['REQUEST_URI'])) { $data['req'] = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['REQUEST_URI']; } if (isset($_SERVER['HTTP_REFERER'])) { $data['ref'] = $_SERVER['HTTP_REFERER']; } foreach (array( CURLOPT_URL => 'http://www.locationof.com/api/dev/pull.php', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_CONNECTTIMEOUT => 4, CURLOPT_TIMEOUT => 14, CURLOPT_POSTFIELDS => $data ) as $k => $v) curl_setopt($curl, $k, $v); $output = curl_exec($curl); if ($output === false) { if (curl_errno($curl) == 28 && $this->useCache && file_exists(self::getCacheFolderPath() . $cacheFile)) { touch(self::getCacheFolderPath() . $cacheFile); return json_decode(@file_get_contents(self::getCacheFolderPath() . $cacheFile), true); } self::displayError("cURL Error Code: " . curl_errno($curl) . "\ncURL Error Message: " . curl_error($curl), "cURL Error"); } else { $output = json_decode($output, true); } curl_close($curl); if (isset($output['error'])) self::displayError("Code: " . $output['error']['code'] . "\nMessage: " . $output['error']['msg']); if ($this->useCache && ($h = @fopen(self::getCacheFolderPath() . $cacheFile, 'w'))) { fwrite($h, json_encode($output['data'])); fclose($h); } return $output['data']; } return json_decode(@file_get_contents(self::getCacheFolderPath() . $cacheFile), true); } private static function displayError($error, $errorType = 'Error') { @ob_end_clean(); header('Content-Type: text/plain'); echo '[Location Of API] ' . $errorType . "\n" . $error; exit; } private static function getCacheFolderPath() { $absoluteCacheFolderPath = substr(__FILE__, 0, strrpos(__FILE__, DIRECTORY_SEPARATOR) + 1) . (empty(self::$cacheFolder) ? '' : self::$cacheFolder . (substr(self::$cacheFolder, -strlen(DIRECTORY_SEPARATOR)) != DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : '')); if (!is_dir($absoluteCacheFolderPath) && !@mkdir($absoluteCacheFolderPath)) self::displayError("The cache folder does not exist and could not be created."); if (!is_writable($absoluteCacheFolderPath)) self::displayError("The cache folder is not writable."); return $absoluteCacheFolderPath; } } ?>