file_get_contents不在localhost上工作,但在在线Web服务器上工作

时间:2015-10-24 17:46:35

标签: php file-get-contents

image of the code

$json = file_get_contents('http://localhost/hts2015/data/functions.php?tag=FIND');


$json = file_get_contents('http://www.hptsprofessional.tk/data/functions.php?tag=FIND');

$jsonD=json_decode($json);
 $no=count($jsonD);

2014年我在localhost上使用相同的代码来使用file_get_contents()访问数据。它对我来说工作正常,它适用于localhost以及更改url它也适用于在线服务器。最后几天我试图访问相同的代码但它没有显示任何结果但是当我从代码中复制url并粘贴到浏览器uri时它会正确显示json数据输出。但我无法使用file_get_contents()访问它。我尝试更改php.ini allow_url_fopen=On但未能从代码中获得预期结果。有人帮我解决了这个问题吗?

1 个答案:

答案 0 :(得分:0)

可能必须指定用户代理标头。试试这个:

$options  = array('http' => array('user_agent' => 'custom user agent string'));
$context  = stream_context_create($options);
$response = file_get_contents('http://domain/path/to/uri', false, $context);

要调试此功能,您可以尝试:

file_get_contents("http://domain/path/to/uri");
var_dump($http_response_header);
祝你好运!

相关问题