Retry added

This commit is contained in:
Daniele Callari 2019-10-04 09:33:10 +02:00
parent 89582e8644
commit 3424680e9b

View File

@ -1,16 +1,29 @@
<?php <?php
class TelegramBot { /*
* PHP Telegram API implementation
* Daniele Callari
* Daxtech.net
*
*/
class TelegramBot
{
# #
# version # version
# #
const Version = '1.1'; const Version = '1.2';
#
# limit of http retry
#
const RetryMax = 5;
# #
# Bot token # Bot token
# #
private $Token = null; private $Token = null;
# #
# bool param # bool param - TODO
# #
private $Async = false; private $Async = false;
@ -32,11 +45,21 @@ class TelegramBot {
# #
# init class # init class
# #
function __construct($token){ function __construct($token)
{
# #
# set token as private # set token as private
# #
$this->Token = $token; $this->Token = $token;
#
# check curl module
#
if( !function_exists ('curl_init') )
{
header("Content-type:application/json");
die(json_encode(array('error' => true, 'message' => 'php-curl module not installed/enabled')));
}
} }
# #
@ -74,18 +97,29 @@ class TelegramBot {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# #
# TODO - manage timeout and retry # do http call with retry
# #
$TryCounter = 0;
$Result = false;
while( $TryCounter < self::RetryMax )
{
# #
# do call # do call
# #
$result = curl_exec($ch); $Result = curl_exec($ch);
#
# manage retry
#
if($Result === false) $TryCounter++;
else $TryCounter = self::RetryMax;
}
# #
# parse response # parse response
# #
$j = json_decode($result, true); $j = @json_decode($Result, true);
# #
# output if debug # output if debug
@ -95,7 +129,7 @@ class TelegramBot {
# #
# return response from api.telegram # return response from api.telegram
# #
return $j; return ($Result)?$j:json_encode([]);
} }
# #