有限的API-循环时通过DO调用所有结果
与此EIA API一起工作,限制为5000个结果。我要做的是循环,当我再次达到5000个结果循环时,直到获取所有结果并放入数组为止。
这是我到目前为止所做的,但页面上仍然是5000。不是实际的数字,因为它们大约为10k。
在任何情况下,我都不知道length
时,我肯定会超过5000。
$offset = 0;
$length = 5000;
$allData = array();
$url = "https://api.eia.gov/v2/international/data/v2/data?api_key=xxxxxxxxxx&start=1960&sort[0][direction]=desc&offset=$offset&length=$length";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
do {
curl_setopt($ch, CURLOPT_URL, $url);
$jsonData = curl_exec($ch);
$response = json_decode($jsonData);
foreach($response->response->data as $row1){
$urlData[] = $row1;
}
$received = $response->total; // total number of results
$offset = $received - $length;
$allData = array_merge($allData, $urlData);
//$offset += $received;
} while ( $offset == $received );
curl_close($ch);
更新:结果
stdClass Object
(
[response] => stdClass Object
(
[count query execution] => 0.029705004
[total] => 9517
[data]
(
.....
)
)
,因此总计应为$ response-响应 - 响应 - > total
,但是当我将其添加到中时);
it time-out
更新2:var_dump($ wenspy)
object(stdClass)#5003 (3) {
["response"]=>
object(stdClass)#5002 (7) {
float(2.6497E-5)
["count query execution"]=>
float(0.037652072)
["total"]=>
int(9517)
["data"]=>
array(0) {
}
}
["request"]=>
["sort"]=>
array(1) {
[0]=>
object(stdClass)#1 (1) {
["direction"]=>
string(4) "desc"
}
}
["offset"]=>
int(9517)
["length"]=>
int(5000)
}
}
["apiVersion"]=>
string(5) "2.0.2"
}
出于某种原因,此设置[“ data”] =>阵列(0)()
是空的,但不应该是。这是实际的(应该返回)返回的
Array
(
[0] => stdClass Object
(
[count query execution] => 0.039096933
[total] => 9517
[data] => Array
(
[0] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
[1] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
..........
[4999] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
)
)
Working with this EIA API with a limit of 5000 results at once. What I'm trying to do is to loop and when I reach 5000 results loop again until all results are fetched and put in an array.
This is what I have done so far but they are still 5000 on the page. Not the actual number since they are around 10k.
In any case I don't know how much will be length
when I call it but it will be more than 5000 for sure.
$offset = 0;
$length = 5000;
$allData = array();
$url = "https://api.eia.gov/v2/international/data/v2/data?api_key=xxxxxxxxxx&start=1960&sort[0][direction]=desc&offset=$offset&length=$length";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
do {
curl_setopt($ch, CURLOPT_URL, $url);
$jsonData = curl_exec($ch);
$response = json_decode($jsonData);
foreach($response->response->data as $row1){
$urlData[] = $row1;
}
$received = $response->total; // total number of results
$offset = $received - $length;
$allData = array_merge($allData, $urlData);
//$offset += $received;
} while ( $offset == $received );
curl_close($ch);
Update: result
stdClass Object
(
[response] => stdClass Object
(
[count query execution] => 0.029705004
[total] => 9517
[data]
(
.....
)
)
So the total should be $response->response->total
but when I add it to the while ( $response->response->total > 0 );
it timed-out
UPDATE 2: var_dump($response)
object(stdClass)#5003 (3) {
["response"]=>
object(stdClass)#5002 (7) {
float(2.6497E-5)
["count query execution"]=>
float(0.037652072)
["total"]=>
int(9517)
["data"]=>
array(0) {
}
}
["request"]=>
["sort"]=>
array(1) {
[0]=>
object(stdClass)#1 (1) {
["direction"]=>
string(4) "desc"
}
}
["offset"]=>
int(9517)
["length"]=>
int(5000)
}
}
["apiVersion"]=>
string(5) "2.0.2"
}
for some reason with this setup ["data"]=> array(0) ()
is empty but should not be. This is actual what is (should return) returning
Array
(
[0] => stdClass Object
(
[count query execution] => 0.039096933
[total] => 9517
[data] => Array
(
[0] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
[1] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
..........
[4999] => stdClass Object
(
[year] => 2021
[Id] => 57
[Name] => name
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
offset
is0
的记录总数5000
(这是您的$长度
)。请参阅下面的片段,其中包含所需的更改。摘要:
offset
is0
for the pagination limit of5000
(which is your$length
). See the below snippet with the changes needed written inside the comments.Snippet: