0

I developed a telegram bot that posts the score of the cricket match in the telegram group. As you know, the score will keep changing within time, So I need to send the message when only the score is updated. To achieve this, I'm using the long-polling method (hit the API after every 10 seconds to get the updated score) but at one time, the maximum execution time is exceeded. So what is the best way to achieve this?

<?php
session_start();

$token = 'xyz';
$group_name = 'xyz';

while(true){

    $ipl = file_get_contents('https://cricket-api.vercel.app/cri.php?url=https://www.cricbuzz.com/live-cricket-scores/38708/rocks-vs-war-pool-d-csa-provincial-t20-cup-2021');  

    $ipl_data = json_decode($ipl, true);
    $current_score = $ipl_data['livescore']['current'];

    $_SESSION["score"] =  $current_score;

    if($_SESSION["score"] != $current_score){
        $bot = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$group_name}&text={$current_score}";
        $hit = file_get_contents($bot);    
    }
    sleep(10);    
}
?>
Shakti Goyal
  • 127
  • 1
  • 8
  • you are exceeding the time limit by your self. you can increase the limit https://www.php.net/manual/en/function.set-time-limit.php – hassan Oct 04 '21 at 10:34
  • 1
    I doubt if that would be considered a reliable way – Shakti Goyal Oct 04 '21 at 10:40
  • 2
    Run the code as a CRON job using the PHP CLI then there are no execution time limits – RiggsFolly Oct 04 '21 at 10:42
  • You also did not implement any mechanism for remembering the last score so you only make updates when the score has changed? – RiggsFolly Oct 04 '21 at 10:45
  • Does this answers your question? [How to increase maximum execution time in php](https://stackoverflow.com/questions/16171132/how-to-increase-maximum-execution-time-in-php) – 0stone0 Oct 04 '21 at 10:45
  • Let me update the code so you can see what I have achieved so far. You can check now @RiggsFolly – Shakti Goyal Oct 04 '21 at 11:16
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Oct 04 '21 at 11:17
  • So where is the `session_start()` in this code? – RiggsFolly Oct 04 '21 at 11:18
  • I forgot to add here! But it is present in the script – Shakti Goyal Oct 04 '21 at 11:19
  • I'm sure I did something silly or have I done it correctly? – Shakti Goyal Oct 04 '21 at 11:20
  • Did you able to figure out what have I done wrong? the solution to compare the new value retrieved from an API call against the previously retrieved value? – Shakti Goyal Oct 04 '21 at 11:33
  • The problem has been fixed! here is the solution https://stackoverflow.com/questions/69437889/store-api-previous-value-and-compare-with-new-one/69438171#69438171 – Shakti Goyal Oct 04 '21 at 16:18

0 Answers0