我无法让它发挥作用。我只想打印前3行(结果)。虽然我可以获得第一排,但这完全不是我想要的。如在pager(per_page)中所见,API会发送总共50个结果(行)。这是我的示例代码..
<?php $url = 'https://example.com/events/inplay?token=2645-sl1uDzY3tVvv9M8873472&sport_id=1';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch) or die(curl_error($ch));
if ($data === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
$obj = json_decode($data, true);
foreach ($obj['results'] as $res){
echo $res['league']['name'];
echo $res['away']['name'];
echo $res['away']['name'];
}
?>
这将输出所有50个结果。但我只想打印前3个结果左右。这里是json解码:::
{
"success":1,
"pager":{
"page":1,
"per_page":50,
"total":43
},
"results":[
{
"id":"584824",
"sport_id":"1",
"time":"1516183519",
"time_status":"1",
"league":{ },
"home":{
"id":"51566",
"name":"Al Fahaheel SC",
"image_id":"55159",
"cc":"kw"
},
"away":{
"id":"51565",
"name":"Al Salibikhaet SC",
"image_id":"55165",
"cc":"kw"
},
"ss":"0-0",
"timer":{ },
"scores":{ },
"extra":{ },
"has_lineup":0,
"inplay_created_at":"1516194754",
"inplay_updated_at":"1516195076",
"confirmed_at":"1516194766"
},
{
"id":"584825",
"sport_id":"1",
"time":"1516183524",
"time_status":"1",
"league":{
"id":"2425",
"name":"Kuwait Crown Prince Cup",
"cc":"kw"
},
"home":{
"id":"49553",
"name":"Al Kuwait SC",
"image_id":"32525",
"cc":"kw"
},
"away":{
"id":"51567",
"name":"Al Shabab Club",
"image_id":"55169",
"cc":"kw"
},
"ss":"0-0",
"timer":{
"tm":0,
"ts":0,
"tt":"0",
"ta":0
},
"scores":{
"2":{
"home":"0",
"away":"0"
}
},
"extra":{ },
"has_lineup":0,
"inplay_created_at":"1516194950",
"inplay_updated_at":"1516195076",
"confirmed_at":"1516195033"
},
{
"id":"574814",
"sport_id":"1",
"time":"1516188580",
"time_status":"1",
"league":{
"id":"760",
"name":"Turkey Cup",
"cc":"tr"
},
"home":{
"id":"43941",
"name":"Akhisar Belediye",
"image_id":"24748",
"cc":"tr"
},
"away":{
"id":"17361",
"name":"Boluspor",
"image_id":"6414",
"cc":"tr"
},
"ss":"1-0",
"timer":{
"tm":89,
"ts":59,
"tt":"1",
"ta":0
},
"scores":{
"2":{
"home":"1",
"away":"0"
}
},
"extra":{ },
"has_lineup":1,
"inplay_created_at":"1516188226",
"inplay_updated_at":"1516195076",
"confirmed_at":"1516188264"
},
{
"id":"593150",
"sport_id":"1",
"time":"1516188608",
"time_status":"1",
"league":{
"id":"5946",
"name":"AFC U23 Championship",
"cc":null
},
"home":{
"id":"174631",
"name":"Syria U23",
"image_id":"49101",
"cc":"sy"
},
"away":{
"id":"81107",
"name":"Vietnam U23",
"image_id":"49111",
"cc":"vn"
},
"ss":"0-0",
"timer":{
"tm":89,
"ts":50,
"tt":"1",
"ta":0
},
"scores":{
"2":{
"home":"0",
"away":"0"
}
},
"extra":{ },
"has_lineup":0,
"inplay_created_at":"1516187775",
"inplay_updated_at":"1516195076",
"confirmed_at":"1516187901"
},
{
"id":"587085",
"sport_id":"1",
"time":"1516188609",
"time_status":"1",
"league":{
"id":"5946",
"name":"AFC U23 Championship",
"cc":null
},
"home":{
"id":"174621",
"name":"South Korea U23",
"image_id":"24259",
"cc":"kr"
},
"away":{
"id":"160603",
"name":"Australia U23",
"image_id":"49021",
"cc":"au"
},
"ss":"3-2",
"timer":{
"tm":91,
"ts":7,
"tt":"1",
"ta":5
},
"scores":{
"2":{
"home":"3",
"away":"2"
}
},
"extra":{ },
"has_lineup":0,
"inplay_created_at":"1516187752",
"inplay_updated_at":"1516195076",
"confirmed_at":"1516192911"
},
{
"id":"593172",
"sport_id":"1",
"time":"1516189759",
"time_status":"1",
"league":{
"id":"8467",
"name":"Bangladesh Independence Cup",
"cc":null
},
"home":{
"id":"48561",
"name":"Abahani Limited",
"image_id":"317511",
"cc":"bd"
},
"away":{
"id":"9244",
"name":"Team BJMC",
"image_id":null,
"cc":"bd"
},
"ss":"1-0",
"timer":{
"tm":68,
"ts":16,
"tt":"1",
"ta":0
},
"scores":{
"2":{
"home":"1",
"away":"0"
}
},
"extra":{ },
"has_lineup":0,
"inplay_created_at":"1516189167",
"inplay_updated_at":"1516195076"
},
任何人的帮助将不胜感激。