/** * Url Shortener function * * @param $url * @param $user_id * * @return string * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function shortenUrl($url, $user_id) { if (empty(self::$token)) { return $url; } if (empty($user_id)) { throw new TelegramException('User id is empty!'); } $cached = BotanDB::selectShortUrl($user_id, $url); if (!empty($cached[0]['short_url'])) { return $cached[0]['short_url']; } $request = str_replace(['#TOKEN', '#UID', '#URL'], [self::$token, $user_id, urlencode($url)], self::$shortener_url); $options = ['http' => ['ignore_errors' => true, 'timeout' => 3]]; $context = stream_context_create($options); $response = @file_get_contents($request, false, $context); if (!filter_var($response, FILTER_VALIDATE_URL) === false) { BotanDB::insertShortUrl($user_id, $url, $response); } else { TelegramLog::debug('Botan.io API replied with error: ' . $response); return $url; } return $response; }