2

I'm trying to load a website into a variable. But if I run my code, it returns an other page than I see in my browser.

Here's my code:

$query = $_GET['q'];
$url = 'https://www.google.com/search?q='.str_replace(' ','+',$query);
$doc = file_get_contents($url, false, $context);
echo $doc;

I see a (I guess older and) other version of Google in my website then when I go to Google's website itsself.

Can anyone help me?

Edit

Here are some screenshots: This is my websiteThis is Google

To be more obviously, I want to scrape Google results. But with the file_get_contents function I get strange classes, where I get on the web (https://www.google.com) I see h3-tags.

  • It would be helpful if you could be a bit more specific about what problems / changes you're seeing when you do this? Maybe some screenshots? It could just be that some CSS etc fails to load when you echo it like this (due to relative links etc), but it's unclear exactly what you are talking about. Also...what is the purpose of this code? – ADyson Dec 19 '19 at 10:40
  • 2
    I guess you are seeing this page, https://jsfiddle.net/5pk18jbu/1/ This is because the scripts that are in the source code are not executed, instead file get contents just reads the source code of the url and returns it. – Did Dec 19 '19 at 10:49
  • Hi Diar Selimi, that's exactly what I see! But do you know how I can get the exactly version of Google? –  Dec 19 '19 at 17:36
  • @JeroenvanRensen yes...just load google directly. It's unclear why you want/need to feed it through PHP like this. What are you actually trying to achieve? Because right now this code does not appear to do anything useful. – ADyson Dec 19 '19 at 20:28
  • This is exactly the same problem I'm getting : if I display the source code of a youtube page I can see width and height of the video, but if I use `file_get_contents` to get width and height programmatically they are absent. @Did 's comment makes sense, but the thing is, we need a solution. – Jose Manuel Abarca Rodríguez Apr 29 '22 at 14:49

0 Answers0