PHP API 和 JSON
PHP API With JSON
我一直在尝试使用 PHP 调用 JSON 提要,目前我使用的代码是
$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;
?>
然而,前 2 个回声不起作用,但第 3 个回声起作用...
摘自 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)"
},
知道为什么它没有显示吗?
您的请求可能不会 return stand_team_name
的结果,因为您正在调用的 API 需要一个 comp_id
参数,该参数未显示在 API 打电话给你表明你正在打。
根据文档,您正在调用的 API 需要一个比赛 ID,如下所示:
http://football-api.com/api/?Action=standings&APIKey=[YOUR_API_KEY]&comp_id=[COMPETITION]
文档:http://football-api.com/documentation/#Standings
不添加 comp_id
会导致以下错误响应:
{"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"}
出于测试目的,请使用文档中所示的 comp_id=1204
并尝试将您的代码更改为以下适合我的代码:
$json = file_get_contents( 'http://football-api.com/api/?Action=standings&APIKey=********&comp_id=1204');
这是一个适合我的完整示例:
<?
$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";
?>
我一直在尝试使用 PHP 调用 JSON 提要,目前我使用的代码是
$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;
?>
然而,前 2 个回声不起作用,但第 3 个回声起作用...
摘自 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)"
},
知道为什么它没有显示吗?
您的请求可能不会 return stand_team_name
的结果,因为您正在调用的 API 需要一个 comp_id
参数,该参数未显示在 API 打电话给你表明你正在打。
根据文档,您正在调用的 API 需要一个比赛 ID,如下所示:
http://football-api.com/api/?Action=standings&APIKey=[YOUR_API_KEY]&comp_id=[COMPETITION]
文档:http://football-api.com/documentation/#Standings
不添加 comp_id
会导致以下错误响应:
{"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"}
出于测试目的,请使用文档中所示的 comp_id=1204
并尝试将您的代码更改为以下适合我的代码:
$json = file_get_contents( 'http://football-api.com/api/?Action=standings&APIKey=********&comp_id=1204');
这是一个适合我的完整示例:
<?
$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";
?>