我需要访问API响应中的所有值,并将它们显示在带有以下列的html表中:“date”,“dimes5”,“score”,“teams”,“rot”,“opening”。
这就是我的尝试:
<?php
$games = 'https://api.import.io/store/connector/3d3b5493-d501-4511-ad8f-43c168833999/_query?input=webpage/url:http%3A%2F%2Famericasline.com%2FStats%2FOdds%2FBasketball%2FNBA&&_apikey=db29c3b561c1433bba0d365d7a9c88273c2fcf188f2cd1d215b7dcec1d9ac1880677b574fee6662ac330c8d5ee5358bcc590b5cfce5be1db4ed163b18ba25bc8389603dff096f62a637c9b4bc296c70d';
$games_json = file_get_contents($games);
$games_array = (array)json_decode($games_json, true);
//echo $games_array['results'][date];
print_r($games_array);
$dt = $games_array['results'];
这是REST API响应:
{
"outputProperties": [
{
"type": "STRING",
"name": "date"
},
{
"type": "STRING",
"name": "rot"
},
{
"type": "STRING",
"name": "teams"
},
{
"type": "STRING",
"name": "opening"
},
{
"type": "STRING",
"name": "dimes5"
},
{
"type": "STRING",
"name": "score"
}
],
"pageUrl": "http://americasline.com/Stats/Odds/Basketball/NBA",
"connectorGuid": "3d3b5493-d501-4511-ad8f-43c168833999",
"connectorVersionGuid": "4727fd71-3b59-4254-8e5e-299da2e43a2f",
"cookies": [
"SESS0d8800e218fbe5dbb1d5b44365860b7a=\"2cca2078486d68291cebf2a7845cdcaa\";Path=\"/\";Domain=\"americasline.com\";Port=\"80\""
],
"results": [
{
"opening": "-6½-105",
"rot": "505",
"teams": "San Antonio",
"score": "0",
"dimes5": "-7-101",
"date": "03/07/2016"
},
{
"opening": "198o-110",
"rot": "506",
"teams": "Indiana",
"score": "0",
"dimes5": "195½o-102",
"date": "7:00 PM"
},
{
"opening": "203½o-110",
"rot": "503",
"teams": "Memphis",
"score": "0",
"dimes5": "203½o-105",
"date": "03/07/2016"
},
{
"opening": "-10-110",
"rot": "504",
"teams": "Cleveland",
"score": "0",
"dimes5": "-12½-105",
"date": "7:00 PM"
},
{
"opening": "212o-110",
"rot": "501",
"teams": "Minnesota",
"score": "0",
"dimes5": "215o-102",
"date": "03/07/2016"
},
{
"opening": "-8½-110",
"rot": "502",
"teams": "Charlotte",
"score": "0",
"dimes5": "-10-102",
"date": "7:00 PM"
},
{
"opening": "209o-110",
"rot": "509",
"teams": "Milwaukee",
"score": "0",
"dimes5": "210o-103",
"date": "03/07/2016"
},
{
"opening": "-6-110",
"rot": "510",
"teams": "Chicago",
"score": "0",
"dimes5": "-7-102",
"date": "8:00 PM"
},
{
"opening": "219½o-110",
"rot": "507",
"teams": "Sacramento",
"score": "0",
"dimes5": "-1-105",
"date": "03/07/2016"
},
{
"opening": "-2-110",
"rot": "508",
"teams": "New Orleans",
"score": "0",
"dimes5": "219o-107",
"date": "8:00 PM"
},
{
"opening": "-2-110",
"rot": "511",
"teams": "LA Clippers",
"score": "0",
"dimes5": "-5-108",
"date": "03/07/2016"
},
{
"opening": "211½o-110",
"rot": "512",
"teams": "Dallas",
"score": "0",
"dimes5": "209o-105",
"date": "8:30 PM"
},
{
"opening": "222½o-110",
"rot": "513",
"teams": "Orlando",
"score": "0",
"dimes5": "224o-108",
"date": "03/07/2016"
},
{
"opening": "-15-110",
"rot": "514",
"teams": "Golden State",
"score": "0",
"dimes5": "-14½-103",
"date": "10:30 PM"
},
{
"rot": "701",
"teams": "Brooklyn",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "702",
"teams": "Toronto",
"score": "0",
"date": "7:30 PM"
},
{
"rot": "703",
"teams": "San Antonio",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "704",
"teams": "Minnesota",
"score": "0",
"date": "8:00 PM"
},
{
"rot": "707",
"teams": "New York",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "708",
"teams": "Denver",
"score": "0",
"date": "9:00 PM"
},
{
"rot": "705",
"teams": "Atlanta",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "706",
"teams": "Utah",
"score": "0",
"date": "9:00 PM"
},
{
"rot": "709",
"teams": "Washington",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "710",
"teams": "Portland",
"score": "0",
"date": "10:00 PM"
},
{
"rot": "711",
"teams": "Orlando",
"score": "0",
"date": "03/08/2016"
},
{
"rot": "712",
"teams": "LA Lakers",
"score": "0",
"date": "10:30 PM"
}
],
"offset": 0
}
答案 0 :(得分:0)
结果是一个数组,所以试试这个
$games_json = file_get_contents($games);
$games_array = json_decode($games_json);
foreach ($games_array->results as $result)
{
echo $result->date;
echo $result->dimes5;
// do the stuffs here with values
}
答案 1 :(得分:0)
看起来相当明显而且直接开始,但我也注意到在结果数组中并非所有字段都显示在所有出现上,所以我在子数组中的每个字段上添加了一点检查,这样你就不会获取错误并添加默认值
,以便在没有数据可用的所有表格单元格中放置一些内容。
<?php
$games = 'https://api.import.io/store/connector/3d3b5493-d501-4511-ad8f-43c168833999/_query?input=webpage/url:http%3A%2F%2Famericasline.com%2FStats%2FOdds%2FBasketball%2FNBA&&_apikey=db29c3b561c1433bba0d365d7a9c88273c2fcf188f2cd1d215b7dcec1d9ac1880677b574fee6662ac330c8d5ee5358bcc590b5cfce5be1db4ed163b18ba25bc8389603dff096f62a637c9b4bc296c70d';
$games_json = file_get_contents($games);
$games_array = (array)json_decode($games_json, true);
//echo $games_array['results'][date];
//print_r($games_array);
echo '<table>';
echo '<tr><td>Date</td><td>dimes5</td><td>score</td><td>rot</td><td>opening</td></tr>';
foreach ( $games_array['results'] as $result) {
$date = isset($result['date']) ? $result['date'] : ' ';
$dimes5 = isset($result['dimes5']) ? $result['dimes5'] : ' ';
$score = isset($result['score']) ? $result['score'] : ' ';
$rot = isset($result['rot']) ? $result['rot'] : ' ';
$opening = isset($result['opening']) ? $result['opening'] : ' ';
echo sprintf('<tr><td>%s<td><td>%s<td><td>%s<td><td>%s<td><td>%s<td></tr>',
$date,
$dimes5,
$score,
$rot,
$opening);
}
echo '</table>';