调用GET请求分页API,从所有页面获取所有数据而不是元数据

时间:2017-10-05 14:25:43

标签: php json

我想从第三方API(json)中检索所有数据,如下所示:

{
  "data": [
    {
      "id": 6585,
      "league_id": 8,
      "season_id": 13,
      "stage_id": 11,
      "round_id": 645,
      "aggregate_id": null,
      "venue_id": 167,
      "referee_id": 1,
      "localteam_id": 65,
      "visitorteam_id": 19,
      "weather_report": {
        "code": "clear",
        "icon": "https://cdn.sportmonks.com/images/weather/01d.png",
        "type": "clear sky",
        "wind": {
          "speed": "8.05 m/s",
          "degree": 180
        },
        "clouds": "0%",
        "humidity": "48%",
        "temperature": {
          "temp": 61.88,
          "unit": "fahrenheit"
        }
      },
      "commentaries": null,
      "attendance": 31474,
      "winning_odds_calculated": false,
      "formations": {
        "localteam_formation": "4-2-3-1",
        "visitorteam_formation": "3-4-2-1"
      },
      "scores": {
        "localteam_score": 0,
        "visitorteam_score": 2,
        "localteam_pen_score": null,
        "visitorteam_pen_score": null,
        "ht_score": "0-0",
        "ft_score": "0-2",
        "et_score": null
      },
      "time": {
        "status": "FT",
        "starting_at": {
          "date_time": "2017-05-10 18:45:00",
          "date": "2017-05-10",
          "time": "18:45:00",
          "timestamp": 1494441900,
          "timezone": "UTC"
        },
        "minute": 90,
        "extra_minute": null,
        "injury_time": null
      }
    },
    ...
  ],
  "meta": {
    ...
  }
}

每天返回不同的总数结果。 每个页面都有不同的页数,我设置为调用API以获取页面的总数

$dateToday=date('Y-m-d');
$url1='https://sitename.com/api/v2.0/fixtures/between/'. $dateToday .'/'. $dateToday .';
$file = file_get_contents($url1);
$data = json_decode($file);
$total_pages = $data->meta->pagination->total_pages;

然后我尝试只从每个页面获取数据而不是元数据。因此我尝试使用以下方法

$data='';
for ($page = 0; $page < $total_pages; $page++) {

$service_url = 'https://sitename.com/api/v2.0/fixtures/between/'. $dateToday .'/'. $dateToday .';

    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($curl);


    $data .= $curl_response;

    $decoded = json_decode($data->data);

}

if ( file_put_contents(API_DIR . "home/matches.json" , $ decoded))):
          endif;    
        die(json_encode(['success' => 1 ]));

但结果为空。

如果更改$ decoding = json_decode($ data);

获取此错误 SyntaxError:JSON.parse:在JSON数据的第1行第1732518行的JSON数据之后出现意外的非空白字符

其中包括所有页面的所有元数据

请问您能告诉我该如何解决此问题以获取数据。

以下是所有代码:

<?php 
$dateToday=date('Y-m-d');
$url1='https://sitename.com/api/v2.0/fixtures/between/'. $dateToday .'/'. $dateToday .';
$file = file_get_contents($url1);
$data = json_decode($file);
$total_pages = $data->meta->pagination->total_pages;
$data='';
for ($page = 0; $page < $total_pages; $page++) {

$service_url = 'https://sitename.com/api/v2.0/fixtures/between/'. $dateToday .'/'. $dateToday .';

              $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($curl);


    $data .= $curl_response;
}
$decoded = json_decode($data);

if ( file_put_contents(API_DIR . "home/matches.json" , $ decoded))):
          endif;    
        die(json_encode(['success' => 1 ]));    

?>

0 个答案:

没有答案