PHP server hitting https://www.google.com/movies gets no results

169 Views Asked by At

If you go to https://www.google.com/movies?near=02215&q=revenant you will clearly get results returned to you that can be parsed. However, when I upload a small script to my server like:

<?php 

$string = file_get_contents("https://www.google.com/movies?near=02215&q=revenant");

echo $string;

?>

The output of the results is something along the lines of "No results found"

Any ideas?

1

There are 1 best solutions below

2
On

Google is picky about who they send HTTP responses to, tell them you're a browser, see if this works:

$context = stream_context_create(array(
    'http' => array(
        'method' => "GET",
        'header' => "" .
            "Accept: text/html" . "\r\n" .
            "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0" . "\r\n"
    )
));

$string = file_get_contents( "https://www.google.com/movies?near=02215&q=revenant", false, $context );