Home > Back-end >  Store API previous value and compare with new one
Store API previous value and compare with new one

Time:10-05

I developed a telegram bot whose job is to post the score of a cricket match in the telegram group. The score needs to be posted only when the new score is updated. I'm trying to store the old score in the session and then compare it with the new one.

If both values (old and new) didn't match, post the score in the group but for some unknown reason, it is not working

Here is my code

<?php

session_start();

set_time_limit(0);


$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/37632/dc-vs-csk-50th-match-indian-premier-league-2021');  

$ipl_data = json_decode($ipl, true);

$current_score = $ipl_data['livescore']['current'];
        
if($current_score != $_SESSION['old_score']){
  $bot = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$group_name}&text={$current_score}";
  $hit = file_get_contents($bot);      
} 

$_SESSION['old_score'] = $current_score;
    


sleep(10);    

}
 

?>

CodePudding user response:

Look at that

$_SESSION['old_score'] = $current_score;

if($current_score != $_SESSION['old_score'])

Your sessions "old_score" always equals $current_score Try first your if, then change session field

if($current_score != $_SESSION['old_score']){
       
} 

$_SESSION['old_score'] = $current_score;
  • Related