Home > Back-end >  How to Combine two api calls and make a loop using php
How to Combine two api calls and make a loop using php

Time:06-12

I am trying to loop an array of fixtures using foreach and this works fine. However I want to pick the {fixtuteID} from the fixtures loop and put it in the predictions api endpoint

Api call for fixtures

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/fixtures?date=2xx1-06-10&timezone=XXXXX",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
    'x-rapidapi-host: xvvvxxx.io'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response; 

Api call for predictions which requires {fixtuteID} from the above

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/predictions?fixture={fixtuteID}",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
    'x-rapidapi-host: xvvvxxx.io'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response; 

Below is the loop I would like.

fixture 1 and prediction-fixture 1

fixture 2 and prediction-fixture 2

fixture 3 and prediction-fixture 3

fixture 4 and prediction-fixture 4

end loop

EDIT: my loop

$fixture = curl_exec($curl);
    $fs = json_decode($fixture, true); 
    $fixtures = $fs['response'];
    //get error 
    $err = curl_error($curl);

    curl_close($curl); 
if ($err) {
        echo "cURL Error #:" . $err;
    } else { 
    foreach($fixtures as $fx){

    //fixture ID {fixtuteID}

         echo $fx['fixture']['id'];

    //I wand to add the prediction here like this-- echo $pr['prediction']."<br>";
         }  
    }

And that loop gives me

fixture 1

fixture 2

fixture 3

fixture 4

EDIT I want to get the

"predictions->winner->id": 2232,->name": "Sao Jose",->comment": "Win or draw"
        },

The json response from the predictions call is as below.

  {
  "get": "predictions",
  "parameters": {
    "fixture": "840715"
  },
  "errors": [
    
  ],
  "results": 1,
  "paging": {
    "current": 1,
    "total": 1
  },
  "response": [
    {
      "predictions": {
        "winner": {
          "id": 2232,
          "name": "Sao Jose",
          "comment": "Win or draw"
        },
        "win_or_draw": true,
        "under_over": null,
        "goals": {
          "home": "-2.5",
          "away": "-1.5"
        },
        "advice": "Double chance : Sao Jose or draw",
        "percent": {
          "home": "50%",
          "draw": "50%",
          "away": "0%"
        }
      },
      "league": {
        "id": 75,
        "name": "Serie C",
        "country": "Brazil",
        "logo": "https://media.api-sports.io/football/leagues/75.png",
        "flag": "https://media.api-sports.io/flags/br.svg",
        "season": 2022
      },

where I want to pick predictions -> winner -> id for each fixture in the loop.

CodePudding user response:

It's not entirely clear where you got stuck with this, but it seems like the neatest way would be to put the cURL code in a function and then call the function from the loop. (Of course you could put the cURL code directly in the looop but it would clutter it up a bit.)

foreach($fixtures as $fx)
{
    //fixture ID
    echo $fx['fixture']['id'];
    echo getPrediction($fx["fixture"]["id"])."<br>";
}

function getPrediction($fixtureID)
{
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/predictions?fixture= 
 $fixtureID",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
      'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
      'x-rapidapi-host: xvvvxxx.io'
    ),
  ));

  $response = curl_exec($curl);

  curl_close($curl);
  return $response;
}
  • Related