0

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.

Daniel Omara
  • 117
  • 2
  • 17
  • Please show the current loop code so we understand where to start from. At the moment it's all a bit out of context and it's unclear why you can't pass the fixture ID to the other cURL call. – ADyson Jun 10 '22 at 14:57
  • I have added the edit as above – Daniel Omara Jun 10 '22 at 15:15
  • `The json response from the predictions call is as below`...that is not valid JSON. Please show the raw JSON, it's not clear where you got that version from. Also it would have been sensible to mention this requirement about getting a specific value from the JSON before. Also, where are you stuck with that requirement? See [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) for guidance on how to approach this kind of task. – ADyson Jun 11 '22 at 17:12
  • @ADyson, check the json edit above – Daniel Omara Jun 11 '22 at 17:26
  • Ok. And where are you stuck with that exactly? Have you tried anything? I already gave you an answer for your original question and now you have asked for an extra thing. Did the original answer help you? Have you attempted anything to solve the next bit - especially using the link I provided? – ADyson Jun 11 '22 at 18:41
  • @ADyson, Thank for your help it worked fine with some modifications – Daniel Omara Jun 12 '22 at 06:53

1 Answers1

0

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;
}
ADyson
  • 57,178
  • 14
  • 51
  • 63