Get Json data with file_get_content with pagination

Asked

Viewed 65 times

0

I am capturing Json data from an external url outside of my domain using file_get_content the url structure and this way https://site.com.br/pg=1 I use the code below to extract this data

function file_get($url) {

   //Realize comunicação com o servidor
   $contents = file_get_contents( $url, null, null);            
   $data = json_decode($contents,true);
        
       return $data;
  }

And I call the function this way passing as parameter the URL to be accessed .

$html = file_get("https://site.com.br/api?pg=1");
 var_dump($html); 

When I run the code everything works perfect .

More like the URL accessed has pagination so I have to access pg=2 to search for more data that comes with the problem php accesses pg=2 plus the return and always the data of pg=1 .

Follow an example below

$html = file_get("https://site.com.br/api?pg=1");
 var_dump($html);

$html = file_get("https://site.com.br/api?pg=2");
 var_dump($html);

If I access the url directly in the browser https://site.com.br/api?pg=1 and https://site.com.br/api?pg=2 the data are returned differently correctly.

I’m no expert in php but I think something is missing as cache closes some connection if you can help me thank you !

  • I would choose to use Curl but see if this helps. https://stackoverflow.com/questions/7352832/get-request-from-php-using-file-get-contents-with-parameters

No answers

Browser other questions tagged

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