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.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功