-3

I am not able to decode this JSON response into PHP using json_decode, your help is appreciated:

JSON response from API

{
      "fixtures": [
        {
          "id": 59757,
          "home_team_id": 24,
          "away_team_id": 18,
          "home_score_penalties": 0,
          "away_score_penalties": 0,
              "formation": {
               "home": null,
               "away": null
              },
          "date_time_tba": false,
          "spectators": null,
          "round_id": 4839
        }
      ]
    }

PHP:

$url = "https://api....";
$data = json_decode(file_get_contents($url), true);
echo $data['fixtures'][0]['home_team_id'];
echo $data['formation'][0]['home'];

I get no result!

thank you.

Basheer
  • 328
  • 1
  • 2
  • 10
  • 1
    Your question is incomplete. What do you mean by `multiple`? Where is the actual code? What do you mean by _I failed to decode..._? – FirstOne Jul 31 '16 at 21:44
  • I'v edited my question, please review, thx. – Basheer Jul 31 '16 at 21:48
  • 1
    If you run `echo '
    '; print_r($data); echo '
    ';` you can see the structure of the array. You can access the first ID like this: `echo $data['fixtures'][0]['id'];`. You can use a loop to show the values, but as I said, your question is not very clear.
    – FirstOne Jul 31 '16 at 21:52
  • I am able to see the whole structure and the values of the arrays using print_r($data), but I need to display specific objects like home_team_id and round_id separately. – Basheer Jul 31 '16 at 21:56
  • I think the question is better now :/ – Basheer Jul 31 '16 at 22:13
  • have you tried this; `echo $data['fixtures']['home_team_id'];`? – atoms Jul 31 '16 at 22:27
  • Does no result mean a blank screen? Please, check [How do I get PHP Errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). That `$data['formation'][0]['home'];` should be showing an error. It should be `echo $data['fixtures'][0]['formation']['home'];` – FirstOne Jul 31 '16 at 22:27
  • yes, still blank screen with 'echo $data['fixtures'][0]['formation']['home'];' and "error_log" file has old php records! – Basheer Jul 31 '16 at 22:33
  • @atoms yes, still blank! – Basheer Jul 31 '16 at 22:36
  • what was the result of `var_dump($data);`? – atoms Jul 31 '16 at 22:40
  • 1
    I do not see how this JSON is valid, for starters `"round_id": 4839,` can't end with a comma. – Kevin Kopf Jul 31 '16 at 22:41
  • @atoms `array(1) { ["data"]=> array(0) { } }` – Basheer Jul 31 '16 at 22:43
  • @Nordenheim yes I corrected it, thx! – Basheer Jul 31 '16 at 22:45
  • 1
    @Basheer listen to Nordenheim! Also check the json is being downloaded by doing `var_dump(file_get_contents($url));` – atoms Jul 31 '16 at 22:46
  • @atoms I got `string(11) "{"data":[]}"` – Basheer Jul 31 '16 at 22:47
  • 1
    it doesnt look like you're recieveing the expected JSON. Can you try the URL to confirm the response, and that its valid – atoms Jul 31 '16 at 22:49
  • @atoms now I got data `{"data":[{"id":629345,"ht_score":"0-0","ft_score":"2-0",...` but still blank in PHP! – Basheer Jul 31 '16 at 22:55
  • 1
    ok so could you post back the reulst of `var_dump(json_decode(file_get_contents($url)));` – atoms Jul 31 '16 at 22:57
  • 2
    the problem is your JSON is probably not valid. Can you please use the validator? If `var_dump` returns `null`, your JSON is 100% invalid. – Kevin Kopf Jul 31 '16 at 23:00
  • 1
    was assuming he'd read that and would give it a go... Seemed strange var_dump on file_get_contents was blank though? – atoms Jul 31 '16 at 23:02
  • @atoms I got full page of data! – Basheer Jul 31 '16 at 23:02
  • 1
    well done! Will put an answer to cover Nordenheim's comments and your issue with file get contents – atoms Jul 31 '16 at 23:02
  • Coming in right now I have no idea why this question has a -4. But I'm turning that into a -3 because as it stands this is a fantastic question. – vhs May 25 '17 at 05:53

2 Answers2

3

There are miriads of JSON validators out there. Use any of them before you use json_decode.

{
    "fixtures": [{
        "id": 59757,
        "home_team_id": 24,
        "away_team_id": 18,
        "home_score_penalties": 0,
        "away_score_penalties": 0,
        "formation": {
            "home": null,
            "away": null
        },
        "date_time_tba": false,
        "spectators": null,
        "round_id": 4839,
    }]
}

Your example is no way valid JSON, as "round_id": 4839, can't have a comma in the end. The valid JSON would be:

{
    "fixtures": [{
        "id": 59757,
        "home_team_id": 24,
        "away_team_id": 18,
        "home_score_penalties": 0,
        "away_score_penalties": 0,
        "formation": {
            "home": null,
            "away": null
        },
        "date_time_tba": false,
        "spectators": null,
        "round_id": 4839
    }]
}

Now this JSON returns an array after it's decoded:

$json = '{"fixtures": [{"id": 59757,"home_team_id": 24,"away_team_id": 18,"home_score_penalties": 0,"away_score_penalties": 0,"formation": {"home": null,"away": null},"date_time_tba": false,"spectators": null,"round_id": 4839}]}';

var_dump(json_decode($json, true));

result:

array(1) {
  ["fixtures"]=>
  array(1) {
    [0]=>
    array(9) {
      ["id"]=>
      int(59757)
      ["home_team_id"]=>
      int(24)
      ["away_team_id"]=>
      int(18)
      ["home_score_penalties"]=>
      int(0)
      ["away_score_penalties"]=>
      int(0)
      ["formation"]=>
      array(2) {
        ["home"]=>
        NULL
        ["away"]=>
        NULL
      }
      ["date_time_tba"]=>
      bool(false)
      ["spectators"]=>
      NULL
      ["round_id"]=>
      int(4839)
    }
  }
}
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Per http://devdocs.io/php/function.file-get-contents replace `true` arg with `FILE_USE_INCLUDE_PATH` when calling `file_get_contents` in PHP > 5. – vhs May 25 '17 at 06:01
2

First check that you are recieving output from var_dump(file_get_contents($url)).

Then verify that the string returned is valid JSON. Explained here by Nordenheim

Once you can confirm its valid, inspect the JSON decoded data to see how to access the correct value.

var_dump(json_decode(file_get_contents($url)));

so the following should be what your looking for;

$data = json_decode(file_get_contents($url)); echo $data['data'][0]['id'];;

Community
  • 1
  • 1
atoms
  • 2,993
  • 2
  • 22
  • 43
  • Now data is valid using JSONLint validator, I still get empty page with echo $data['fixtures']['home_team_id']; – Basheer Jul 31 '16 at 23:17
  • 1
    Can you paste contents of last var_dump into your question? – atoms Jul 31 '16 at 23:19
  • here are first 3 lines from browser: `{"data":[{"id":629345,"ht_score":"0-0","ft_score":"2-0","et_score":null,"home_team_id":1266,"away_team_id":1275,"home_score":2,"away_score":0,"home_score_penalties":0,"away_score_penalties":0,"formation":{"home":null,"away":null},"date_time_tba":0,"spectators":null,"starting_date":"2016-07-31","starting_time":"13:30:00","status":"FT","minute":90,"extra_minute":0,"competition_id":114,"venue_id":null,"season_id":511,"round_id":7292,"stage_id":873,"aggregate":null,"placeholder":false},` – Basheer Jul 31 '16 at 23:26
  • using `$data = json_decode(file_get_contents($url)); echo $data['data'][0]['id'];` I got this error: PHP Fatal error: Cannot use object of type stdClass as array in /home/... – Basheer Jul 31 '16 at 23:42
  • 1
    try to cast the output to an array instead of an object; `$data = json_decode(file_get_contents($url), true); echo $data['data'][0]['id'];` – atoms Jul 31 '16 at 23:52