PHP服务器点击https://www.google.com/movies没有结果

时间:2016-01-20 16:40:27

标签: php search search-engine file-get-contents movies

如果你转到https://www.google.com/movies?near=02215&q=revenant,你会清楚地得到可以解析的结果。但是,当我将一个小脚本上传到我的服务器时,如:

<?php 

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

echo $string;

?>

结果的输出类似于“未找到结果”

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

谷歌对他们发送HTTP响应的人很挑剔,告诉他们你是浏览器,看看是否有效:

$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 );