Picking up API data

Asked

Viewed 130 times

-1

Good Morning,

I am taking data from an API and it returns me 100 records per page, however it does not tell me how many pages there are of record. The only information that is passed to me is when you find no more data, as an example below;

{
"retorno": {
    "erros": [
        {
            "erro": {
                "cod": 14,
                "msg": "A informacao desejada nao foi encontrada"
            }
        }
    ]
}

}

What I’m not able to do is paging to get the data until the API returns the error. Below is part of my code.

        $apikey = "{apikey}";
    $outputType = "json";
    $page = 1;
    $url = 'https://site.com.br/Api/v2/pedidos/page=' .$page.  '/' . $outputType;
    $retorno =  $this->executeGetContacts($url, $apikey);
    foreach($retorno as $erro){
        if(isset($erro->erros[0]->erro->cod) == 14){
            echo ('Pedidos cadastrados');
        }else{
            foreach($retorno as $pedidos){
                for($page; $page < count(); $page++)
            }
        }
    }

Thank you very much

  • Version of Laravel?

  • I’m using the 5.8

  • Laravel already has paginate if you want to put an example code

  • It wouldn’t be paginar. What I want to do is when the API I’m picking up the data, return me the 100 records the variable $page has to go incrementing to get the other 100 data from the API, until the API returns me the code 14 and falls into the if

  • you can only pass a data limit in the query and returns as much right without this whole revolution

  • Could you give me an example?

Show 1 more comment

1 answer

0

Example of queries with limit in the Laravel:

Model::limit(10)->get();

Run a test and let me know.

  • Lucas. I can’t use limit. I’m taking data from an API. It returns me a json with 100 data per page. To get more 100 data I have to use https://site.com.br/Api/v2/pedidos/page=2/json and so on, until it returns me code 14 (no other data found). The problem I’m having is incrementing the page. It doesn’t go beyond 2. It would have to increase until it fell into the if

Browser other questions tagged

You are not signed in. Login or sign up in order to post.