0

I am trying to parse this JSON from a Football API using PHP.

Below is a subset of the JSON output.

Specifically, I am trying to get the fixture id (eg. 198772) from the below json.

$json = '{
    "get": "predictions",
    "parameters": {
        "fixture": "198772"
    },
    "errors": [],
    "results": 1,
    "paging": {
        "current": 1,
        "total": 1
    },
    "response": [{
        "predictions": {
            "winner": {
                "id": 1189,
                "name": "Deportivo Santani",
                "comment": "Win or draw"
            },
            "win_or_draw": true,
            "under_over": "-3.5",
            "goals": {
                "home": "-2.5",
                "away": "-1.5"
            },
            "advice": "Combo Double chance : Deportivo Santani or draw and -3.5 goals",
            "percent": {
                "home": "45%",
                "draw": "45%",
                "away": "10%"
            }
        }
    }]
}';

$response = json_decode($json);
echo "Output: ". $response[0]->fixture; 

When I tried to run my codes, I encounter this error:

Attempt to read property "fixture" on string in C:\xampp\htdocs\xampp\livescore\api-test\predictions.php on line 90

This is the liner that is throwing up the error:

    echo "Output: ". $response[0]->fixture;

Any idea why this is happening? Any help or insights is much appreciated.

hygull
  • 8,464
  • 2
  • 43
  • 52
  • 1
    `echo "Output: ". $response->parameters->fixture;` – Daniel May 30 '21 at 16:51
  • thanks Daniel. It worked perfectly. Can you also show me how to get the home id? I tried this - echo "Output: ". $response->response->predictions->percent->home.'
    '; but it didnt work. --------------------------------------------Warning: Attempt to read property "predictions" on array in C:\xampp\htdocs\xampp\livescore\api-test\predictions.php on line 92 Warning: Attempt to read property "percent" on null in C:\xampp\htdocs\xampp\livescore\api-test\predictions.php on line 92
    – Blackstone May 30 '21 at 17:13

0 Answers0