【问题标题】:Using Json to get data from results:next_page使用 Json 从结果中获取数据:next_page
【发布时间】:2013-05-06 18:47:22
【问题描述】:

我目前有一个限制问题,即每个 Curl 请求我得到 1000 个结果,这里的问题是我需要得到大约 7000 个结果。

curl获取的数据是Json格式,数据底部有一个获取下一页数据的链接,每页包含1000条结果。

是否可以做一个数组或某种循环,从每个页面获取所有数据,直到最后一页底部不再有 next_page URL。

我尝试使用的代码示例是:

function curlWrap($url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
}

我只是想找到一种添加数组或循环的方法,以不断从 next_page url 获取结果/数据,直到页面底部不再有 next_page url。

关于 json 布局的更多细节:

json键是“next_page”&“end_time”:

"end_time": ("unix-timestamp") 
"next_page": "https:url/"unix-timestamp" 

每个页面之后都有一个新的时间戳,它将列在键“end_time”中。

这将被列在“next_page”网址的末尾。这两个key都是json代码的最后2行,json文件一共大约有20000行数据。

如果需要更多详细信息,请告诉我,提前谢谢。

【问题讨论】:

    标签: php html json zendesk


    【解决方案1】:

    在不知道 json 布局的情况下。 url有多深,key是什么等

    类似的东西。

    function getNext($jsonIn){
        if (!empty($jsonIn['url'])) {
            curlWrap($jsonIn['url'], $json, $action);
        }  
    }
    
    //-------------
    
    getNext($decoded);
    

    【讨论】:

    • 我添加了一些关于 json 的更多细节,让我知道这些数据是否足够或者我是否应该添加更多。感谢您迄今为止的帮助。
    • 终于搞定了,需要使用decoded并确保大于json end_time: function getNext($decoded,$time){ if ((int) $decoded->end_time > $time ) { $newdecoded = curlWrap("URL".$decoded->end_time, null, "GET");返回 $newdecoded; } 否则 { 返回空值;感谢您的帮助 unasAquila 的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多