2015-08-14 77 views
1

我试着通过Google Hotel Finder检查一家酒店是否可用。我用下面的PHP代码:Google Hotel Finder,file_get_contents PHP

<?php 
//get date, in the future with $_POST 

//get source 
$source = file_get_contents('https://www.google.co.uk/hotelfinder/#search;l=london;d=2015-08-14;n=1;usd=1;h=17709217511794056234;si=;av=d'); 

//"Book a room" only shows when room(s) are available 
if (strpos($source, "Book a room") !== false) { 
    echo "Room(s) available"; 
} else { 
    echo "Nothing available"; 
} 

echo $source; 
?> 

当我在我的服务器上运行此代码,谷歌酒店搜索给了我下面的错误信息:“谷歌的Hotel Finder还没有为您的浏览器进行优化为获得最佳效果,请试用Chrome,Firefox 3.5+,Internet Explorer 8+,Safari 4+“。 因此,谷歌检测到我正在访问页面低谷...有没有办法忽略或绕过这个“块”?

+0

不知道如何检查,但尝试发送一个用户代理。 – chris85

+3

是的,停止搜索谷歌页面,这是明确反对他们的TOS –

+0

不想写一个“大规模”刮脚本。只适用于我自己的网站。 –

回答

1

您可以尝试发送带有标题的用户代理信息。

$options = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net 
       "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
) 
); 

$context = stream_context_create($options); 
$file = file_get_contents($url, false, $context); 

PHP file_get_contents() and headers