dtfo55908 2014-04-24 17:53
浏览 52
已采纳

PHP / MySQL / JSON - 循环遍历JSON响应的所有页面

I am calling the Crunchbase API and it gives me long response, so long that the response has multiple pages that can be accessed with ?page=# at the end of the api url.

My question is how do I write some code to run the script once and it will go through all of the pages available without me having the change the page number every time I call the script?

Simplified version of my code:

$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);

}
  • 写回答

1条回答 默认 最新

  • doulian7305 2014-04-24 18:06
    关注

    If you want to make multiple requests you have to use loops or explicitly get all the pages.

    $numberOfPages = 100;
    for($i = 1; $i < $numberOfPages; $i++) {
        $url = sprintf("https://api.url.com/tags/?page=%d", $i);
    
        // Rest of the code.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了
  • ¥15 gazebo-rviz教程
  • ¥15 付费求做一个自助抢单插件
  • ¥15 bat批处理,关于数据复制问题
  • ¥50 同步两个不同结果的array中某些属性
  • ¥15 悬赏15远程操控解决问题
  • ¥15 CST复制的模型无法单独修改参数?
  • ¥15 前端页面想做个定时任务,但是使用requestAnimationFrame,setinterval和settimeout都不行
  • ¥15 根据以下文字信息,做EA模型图