0

I have been trying to call a JSON feed using PHP the code I have used so far is

$json = file_get_contents( 'http://football-api.com/api/?Action=standings&APIKey=********' );
$team_data = json_decode($json);

<?php 
echo $team_data->teams[0]->stand_team_name;
echo $team_data->teams[1]->stand_team_name;

echo $team_data->APIRequestsRemaining;

?>

However the first 2 echos don't work but the 3rd one does...

A snippet from the API...

{
APIVersion: 1,
APIRequestsRemaining: 1000,
DeveloperAuthentication: "TRUE",
teams: [
{
stand_id: "12049092",
stand_competition_id: "1204",
stand_season: "2014/2015",
stand_round: "29",
stand_stage_id: "12041081",
stand_group: "",
stand_country: "England",
stand_team_id: "9092",
stand_team_name: "Chelsea",
stand_status: "same",
stand_recent_form: "DWDWW",
stand_position: "1",
stand_overall_gp: "28",
stand_overall_w: "19",
stand_overall_d: "7",
stand_overall_l: "2",
stand_overall_gs: "58",
stand_overall_ga: "23",
stand_home_gp: "14",
stand_home_w: "11",
stand_home_d: "3",
stand_home_l: "0",
stand_home_gs: "28",
stand_home_ga: "6",
stand_away_gp: "14",
stand_away_w: "8",
stand_away_d: "4",
stand_away_l: "2",
stand_away_gs: "30",
stand_away_ga: "17",
stand_gd: "35",
stand_points: "64",
stand_desc: "Promotion - Champions League (Group Stage)"
},

Any ideas why it is not showing up?

4
  • I think $team_data is an array when you use json_decode method, isn't it ? Then try to print $team_data with print_r for example and then try to access with $team_data['team'][0].. Commented Mar 20, 2015 at 8:47
  • Is that snippet something you actually received, or a sample from the documentation? Have you tried print_r($team_data)? Commented Mar 20, 2015 at 8:57
  • I was following this guide - webtutsdepot.com/2009/08/31/how-to-read-json-data-with-php Where exactly would the print_r($team_data) go? Commented Mar 20, 2015 at 9:47
  • Anywhere after $team_data = ..; Commented Mar 20, 2015 at 10:17

1 Answer 1

1

Your request may not return a result for stand_team_name because the API you are calling requires a comp_id parameter which isn't shown in the API call you indicate you are making.

From the documentation, the API you are calling requires a competition id as shown below:

http://football-api.com/api/?Action=standings&APIKey=[YOUR_API_KEY]&comp_id=[COMPETITION]

Documentation: http://football-api.com/documentation/#Standings

Not adding the comp_id results in the following error response:

{"APIVersion":1,"APIRequestsRemaining":999,"DeveloperAuthentication":"TRUE","Action":"standings","Params":{"Action":"standings","APIKey":"xxxxxx"},"ComputationTime":0.0018100738525391,"IP":"xxxxxx","ERROR":"You have not entered the competition id, please use the parameter comp_id","ServerName":"Football-API","ServerAddress":"http:\/\/football-api.com\/api"}

For testing purposes, use comp_id=1204 as shown in the documentation and try changing your code to the following which works for me:

$json = file_get_contents( 'http://football-api.com/api/?Action=standings&APIKey=********&comp_id=1204');

Here's a complete example that works for me:

<?
    $api_key = 'xxxxxx';

    $json = file_get_contents( 'http://football-api.com/api/?Action=standings&APIKey=' . $api_key .'&comp_id=1204');

    $team_data = json_decode($json);

    print_r($team_data) . "\n";

    echo $team_data->teams[0]->stand_team_name . "\n";
    echo $team_data->APIRequestsRemaining . "\n";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry where I tried to edit out the API key it deleted the competition ID aswell it currently reads as you mentioned above
I updated the above with the complete code that works for me. Just change the value of $api_key to your api key. The print_r($team_data) will show you what you are getting in the response body to verify there are entries to be displayed.
Thats sorted it! Thanks for your help much appreciated :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.