0

如果您访问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;

?>

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

有任何想法吗?

4

1 回答 1

1

Google 对他们向谁发送 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 );
于 2016-01-20T16:55:35.660 回答