Won*_*Bae 4 php caching wikipedia screen-scraping
file_get_contents('https://en.wikipedia.org/wiki/Category:Upcoming_singles');
Run Code Online (Sandbox Code Playgroud)
返回一个不同的结果(2产品)
而使用Chrome访问同一地址会返回4个产品.
经过检查,我怀疑这可能与此有关
使用...时间戳保存在解析器缓存键中...
在html中返回.我使用时的时间戳较旧file_get_contents()
有关如何使用最新信息的任何想法file_get_contents()?
谢谢!
假设file_get_contents正在发出http请求,最好检查指定的用户代理.
我听说过使用某些用户代理获取数据时出现问题.看看这个问题.
您可以使用流上下文指定其他选项(包括用户代理):
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
Run Code Online (Sandbox Code Playgroud)
看一下file_get_contents 文档.
另外,正如杰克所说,cURL是一个更好的选择.
编辑:
你弄错了.您要添加的是不同的用户代理.例如,使用mozilla firefox中的用户代理可以获得4个结果:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; es-AR; rv:1.9.2.23) Gecko/20110921 Ubuntu/10.10 (maverick) Firefox/3.6.23"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://en.wikipedia.org/wiki/Category:Upcoming_singles', false, $context);
print $file;
Run Code Online (Sandbox Code Playgroud)
但是,我认为这不是"合法的",欺骗它并不好.我认为必须有维基百科提供的任何其他用户代理从外部应用程序获取其数据.