Commit 98f83262 authored by Armando Lüscher's avatar Armando Lüscher

Improve HTTP request handling.

Use a single Guzzle Client instance to handle requests.
Improve debug logging mechanism.
parent fce67969
...@@ -30,7 +30,14 @@ class Request ...@@ -30,7 +30,14 @@ class Request
* *
* @var string * @var string
*/ */
private static $api_uri = 'https://api.telegram.org/bot#APIKEY/#ACTION'; private static $api_base_uri = 'https://api.telegram.org';
/**
* Guzzle Client object
*
* @var \GuzzleHttp\Client
*/
private static $client;
/** /**
* Input value of the request * Input value of the request
...@@ -85,6 +92,7 @@ class Request ...@@ -85,6 +92,7 @@ class Request
{ {
if (is_object($telegram)) { if (is_object($telegram)) {
self::$telegram = $telegram; self::$telegram = $telegram;
self::$client = new Client(['base_uri' => self::$api_base_uri]);
} else { } else {
throw new TelegramException('Telegram pointer is empty!'); throw new TelegramException('Telegram pointer is empty!');
} }
...@@ -161,30 +169,25 @@ class Request ...@@ -161,30 +169,25 @@ class Request
*/ */
public static function execute($action, array $data = null) public static function execute($action, array $data = null)
{ {
$client = new Client(); $debug_handle = (TelegramLog::isDebugLogActive()) ? fopen('php://temp', 'w+') : false;
$debug = (TelegramLog::isDebugLogActive()) ? fopen('php://temp', 'w+') : false;
$post_url = str_replace(
['#APIKEY', '#ACTION'],
[self::$telegram->getApiKey(), $action],
self::$api_uri
);
$post_data = [
'debug' => $debug,
'form_params' => $data,
];
try { try {
$response = $client->post($post_url, $post_data); $response = self::$client->post(
'/bot' . self::$telegram->getApiKey() . '/' . $action,
['debug' => $debug_handle, 'form_params' => $data]
);
} catch (RequestException $e) { } catch (RequestException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} } finally {
//Logging verbose debug output
//Logging verbose debug output if ($debug_handle !== false) {
if ($debug !== false) { rewind($debug_handle);
rewind($debug); TelegramLog::debug(sprintf(
TelegramLog::debug(sprintf("Verbose HTTP Request output:\n%s\n", stream_get_contents($debug))); "Verbose HTTP Request output:\n%s\n",
fclose($debug); stream_get_contents($debug_handle)
));
fclose($debug_handle);
}
} }
$result = $response->getBody(); $result = $response->getBody();
...@@ -215,37 +218,28 @@ class Request ...@@ -215,37 +218,28 @@ class Request
if (!is_dir($dirname) && !mkdir($dirname, 0755, true)) { if (!is_dir($dirname) && !mkdir($dirname, 0755, true)) {
throw new TelegramException('Directory ' . $dirname . ' can\'t be created'); throw new TelegramException('Directory ' . $dirname . ' can\'t be created');
} }
//Open file to write
$fp = fopen($loc_path, 'w+');
if ($fp === false) {
throw new TelegramException('File can\'t be created');
}
$ch = curl_init();
if ($ch === false) {
throw new TelegramException('Curl failed to initialize');
}
$curlConfig = [ $debug_handle = (TelegramLog::isDebugLogActive()) ? fopen('php://temp', 'w+') : false;
CURLOPT_URL => 'https://api.telegram.org/file/bot' . self::$telegram->getApiKey() . '/' . $path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 0,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FILE => $fp,
];
curl_setopt_array($ch, $curlConfig); try {
$result = curl_exec($ch); $response = self::$client->get(
if ($result === false) { '/file/bot' . self::$telegram->getApiKey() . '/' . $path,
throw new TelegramException(curl_error($ch), curl_errno($ch)); ['debug' => $debug_handle, 'sink' => $loc_path]
);
} catch (RequestException $e) {
throw new TelegramException($e->getMessage());
} finally {
//Logging verbose debug output
if ($debug_handle !== false) {
rewind($debug_handle);
TelegramLog::debug(sprintf(
"Verbose HTTP File Download Request output:\n%s\n",
stream_get_contents($debug_handle)
));
fclose($debug_handle);
}
} }
//Close curl
curl_close($ch);
//Close local file
fclose($fp);
return (filesize($loc_path) > 0); return (filesize($loc_path) > 0);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment