How to get a page from another site using PHP

Asked

Viewed 248 times

-1

Hello, I’m trying to get the prices of a page on the iFood site using PHP so that if my client changes the prices he put on their page, the site will already make the automatic correction. The problem is that nothing I try works. I am using the file_get_contents() and still does not load. Ex:

$contents = file_get_contents('https://www.ifood.com.br/delivery/belo-horizonte-mg/puro-sabor-salgados---carlos-prates-carlos-prates');
print_r($contents);

Does anyone know any way that I can capture this page? I just need to pull its content to a variable that the rest I do the treatment...

  • The file_get_contents should work. Returns nothing, some error or warning?

  • Your code is returning the whole page HTML.

  • Not returning the page

  • It’s best to do the reverse, update your customer and your site inject the information into iFood. By coincidence I’m with 3 customers currently leaving iFood for a system of their own precisely because iFood ignored a proposal that I had API (answered with a canned email, I decided to discard them and make a system of my own, after all I don’t even think their product is so good) - And to improve, with their own system customers profit more in sales, without having to pay iFood. Jai delete this comment here.

  • 1

    Another thing, for this type of application usually use the lib curl is more efficient because all get management, post, cookies etc is kind of ready to use. Generally PHP comes with curl installed.

1 answer

-1

I’ve been looking at the Ifood website. What happens is that the website detects which browser is ordering the page before submitting the content. When it identifies that it is PHP, it returns an error.

I tried to circumvent the process with a Stream but it didn’t work either.

<?php

// configura o stream e as opções de Headers(cabeçalhos)
$stream = stream_context_create(
Array("http" => Array("method"  => "GET", 
  "timeout" => 30, 
  "header"  => "User-agent:".$_SERVER['HTTP_USER_AGENT'],
  "Accept"=> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding"=> " gzip, deflate, br",
"Accept-Language"=> " pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
"Cache-Control"=> " max-age=0",
"Connection"=> " keep-alive",
"Host"=> " www.ifood.com.br",
"Upgrade-Insecure-Requests"=> 1
                                ))); 


if ( $arquivo = fopen("https://www.ifood.com.br/delivery/belo-horizonte-mg/puro-sabor-salgados---carlos-prates-carlos-prates", 'r', false, $stream) ) { 
} 

while(!feof($arquivo))
  {
   //Mostra uma linha do arquivo
   $linha = fgets($arquivo);
   echo $linha;
  }?> 
  • Hmmm suspected it might be something like this...

  • 1

    That answer is wrong, that’s another problem. To better understand just browse the site with the developer tools open, and look at the "network" tab. Then you can see how the site mounts the page and simulate exactly the same steps.

Browser other questions tagged

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