0

I have written code, in which i am fetching data from api and storing it to firebase. my file is sending data to firebase. Now i need to run this file again and again for every minute on a cpanel server, thing is that i want to improve this code for optimazing execution time.This code shows fatel error that exceeds exection time for 30sec google firebase apis...etc

 <?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . '/app3-e0228-firebase-adminsdk-7cjy5-e9bf89d01d.json');
$firebase = (new Factory)
   ->withServiceAccount($serviceAccount)
   ->withDatabaseUri('https://app3-e0228-default-rtdb.firebaseio.com')
   ->create();
$database = $firebase->getDatabase();
$dbname = "app3-e0228-default-rtdb";
$url="https://cricket.sportmonks.com/api/v1/livescores?....";
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$output = curl_exec($ch);  
curl_close($ch);
$data = json_decode($output);
$data = json_decode($output);
 $status =  "";
 $note =  "";
 $toss_won_team_id =  "";
 $elected =  "";
 $name1 =  "";
 $image_path1 =  "";
$score1 =  "";
 $wickets1 =  "";
 $overs1 =  "";
 $name2 = "";
 $image_path2 =  "";
 $score2 = "";
 $wickets2 =  "";
 $overs2 =  "";
 if(!empty($data->data[0])){
     $toss_won_team_id =  $data->data[0]->attributes->toss_won_team_id;  
     $localteam_id =  $data->data[0]->attributes->localteam_id;
     $visitorteam_id =  $data->data[0]->attributes->visitorteam_id;
     $elected =  $data->data[0]->attributes->elected;
     if($elected=='batting'&& $toss_won_team_id==$localteam_id){
        $name1 =  $data->included[0]->attributes->name;
        $image_path1 =  $data->included[0]->attributes->image_path;
        $name2 =  $data->included[1]->attributes->name;
        $image_path2 =  $data->included[1]->attributes->image_path;

     }elseif($elected=='batting'&& $toss_won_team_id==$visitorteam_id){
        $name1 =  $data->included[1]->attributes->name;
        $image_path1 =  $data->included[1]->attributes->image_path;
        $name2 =  $data->included[0]->attributes->name;
        $image_path2 =  $data->included[0]->attributes->image_path;
     }
     elseif($elected=='bowling'&& $toss_won_team_id==$localteam_id){
        $name1 =  $data->included[1]->attributes->name;
        $image_path1 =  $data->included[1]->attributes->image_path;
        $name2 =  $data->included[0]->attributes->name;
        $image_path2 =  $data->included[0]->attributes->image_path;
    }elseif($elected=='bowling'&& $toss_won_team_id==$visitorteam_id){

        $name1 =  $data->included[0]->attributes->name;
        $image_path1 =  $data->included[0]->attributes->image_path;
        $name2 =  $data->included[1]->attributes->name;
        $image_path2 =  $data->included[1]->attributes->image_path;

    }
    $note =  $data->data[0]->attributes->note;
    $status =  $data->data[0]->attributes->status;

    $score1 =  $data->included[2]->attributes->score;
    $wickets1 =  $data->included[2]->attributes->wickets;
    $overs1 =  $data->included[2]->attributes->overs;

    $score2 =  $data->included[3]->attributes->score;
    $wickets2 =  $data->included[3]->attributes->wickets;
    $overs2 =  $data->included[3]->attributes->overs;

 $database->getReference()->getChild($dbname)->getChild("status")->set($status);
 $database->getReference()->getChild($dbname)->getChild("note")->set($note);
$database->getReference()->getChild($dbname)->getChild("toss_won_team_id")->set($toss_won_team_id);
 $database->getReference()->getChild($dbname)->getChild("elected")->set($elected);
 $database->getReference()->getChild($dbname)->getChild("name1")->set($name1);
 $database->getReference()->getChild($dbname)->getChild("image_path1")->set($image_path1);
 $database->getReference()->getChild($dbname)->getChild("score1")->set($score1);
 $database->getReference()->getChild($dbname)->getChild("wickets1")->set($wickets1);
 $database->getReference()->getChild($dbname)->getChild("overs1")->set($overs1);
 $database->getReference()->getChild($dbname)->getChild("name2")->set($name2);
 $database->getReference()->getChild($dbname)->getChild("image_path2")->set($image_path2);
 $database->getReference()->getChild($dbname)->getChild("score2")->set($score2);
 $database->getReference()->getChild($dbname)->getChild("wickets2")->set($wickets2);
 $database->getReference()->getChild($dbname)->getChild("overs2")->set($overs2);
 if ($database->getReference($dbname)->getSnapshot()->hasChild("score1"))
 {
     $abc = $database->getReference($dbname)->getChild("score1")->getValue();
     print_r($abc);
 }
 else 
 echo 'no';
?>

I will do cronejobs for this code on capanel server. Currently i am running this on localhost server. will it impact to server later or not and kindly guide me if i want to perform crone jobs on my file what are the requirements. i have configure js-composer with my localhost vscode should i need to install js-composer to run this file on namecheap C-panel hosting server?

Qasim Ali
  • 500
  • 1
  • 5
  • 27

0 Answers0