有限的API-循环时通过DO调用所有结果

发布于 2025-02-10 01:11:08 字数 3173 浏览 2 评论 0原文

与此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-02-17 01:11:08
  • 仅创建一个URL模板不会更改其更改上的变量值。每次更新它们时,您都必须设置它们。
  • 您还必须循环直到特定offset is 0的记录总数 5000(这是您的$长度)。请参阅下面的片段,其中包含所需的更改。

摘要:

<?php

$ch = curl_init();
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, "https://api.eia.gov/v2/international/data/v2/data?api_key=xxxxxxxxxx&start=1960&sort[0][direction]=desc&offset=$offset&length=$length"); #Change-1

    $jsonData = curl_exec($ch);
    $response = json_decode($jsonData);
            
    foreach($response->response->data as $row1){
        $allData[] = $row1; #Change-2 
    }

    $offset += count($response->response->data); // #Change-3

} while ( count($response->response->data) > 0 ); #Change-4

curl_close($ch);
  • Just creating a URL template once doesn't update the variable values on it's changes. You will have to set them each time you update them.
  • You will also have to loop till the total number of records fetched for a particular offset is 0 for the pagination limit of 5000 (which is your $length). See the below snippet with the changes needed written inside the comments.

Snippet:

<?php

$ch = curl_init();
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, "https://api.eia.gov/v2/international/data/v2/data?api_key=xxxxxxxxxx&start=1960&sort[0][direction]=desc&offset=$offset&length=$length"); #Change-1

    $jsonData = curl_exec($ch);
    $response = json_decode($jsonData);
            
    foreach($response->response->data as $row1){
        $allData[] = $row1; #Change-2 
    }

    $offset += count($response->response->data); // #Change-3

} while ( count($response->response->data) > 0 ); #Change-4

curl_close($ch);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文