【问题标题】:PHP/MySQL/JSON - Looping through all pages of JSON responsePHP/MySQL/JSON - 遍历所有 JSON 响应页面
【发布时间】:2014-04-24 17:53:30
【问题描述】:

我正在调用 Crunchbase API,它给了我很长的响应,以至于响应中有多个页面可以在 api url 的末尾使用 ?page=# 访问。

我的问题是我如何编写一些代码来运行脚本一次,它会遍历所有可用的页面,而我每次调用脚本时都无需更改页码?

我的代码的简化版:

$url = "https://api.url.com/tags/?page=2";
$jsondata = file_get_contents($url);
$array = json_decode($jsondata,true);

var_dump($array);

foreach($array as $key => $value) {

mysql_query(" INSERT into cbcompanies (
  `column1`)

VALUES (
  '{$value['foo']}') ",$con);

}

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    如果要发出多个请求,则必须使用循环或显式获取所有页面。

    $numberOfPages = 100;
    for($i = 1; $i < $numberOfPages; $i++) {
        $url = sprintf("https://api.url.com/tags/?page=%d", $i);
    
        // Rest of the code.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2021-05-04
      • 2020-11-30
      • 1970-01-01
      相关资源
      最近更新 更多