dtfo55908 2014-04-24 09: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 10: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.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部