假设file_get_contents 正在发出http 请求,最好检查指定的用户代理。
我听说使用某些用户代理获取数据时出现问题。看看this question。
您可以使用流上下文指定其他选项(包括用户代理):
<?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);
看看file_get_contents docs。
另外,正如 Jack 所说,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;
但是,我认为这不“合法”,因此作弊是不好的。我认为维基百科必须提供任何其他用户代理来从外部应用程序获取其数据。