[英]file_get_contents not working on localhost but working on online web server
$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);
Year 2014 I was use same code on my localhost to access data using file_get_contents()
. 2014年我在localhost上使用相同的代码来使用
file_get_contents()
访问数据。 It was working fine for me, it works on localhost as well as by changing url it works on online server also. 它对我来说工作正常,它适用于localhost以及更改url它也适用于在线服务器。 Last Few days I am trying to access same code but it doesn't show any result but when I copied url from the code and paste at browser uri it show me json data output correctly.
最后几天我试图访问相同的代码但它没有显示任何结果但是当我从代码中复制url并粘贴到浏览器uri时它会正确显示json数据输出。 But I can't access it by using
file_get_contents()
. 但我无法使用
file_get_contents()
访问它。 I am tried to change php.ini
allow_url_fopen=On
but fail to get expected result from code. 我试图更改
php.ini
allow_url_fopen=On
但无法从代码中获得预期的结果。 Is anybody help me to resolve this problem? 有人帮我解决这个问题吗?
It might be mandatory to specify the user agent header. 可能必须指定用户代理标头。 Try this:
尝试这个:
$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);
To debug this you can try: 要调试它,您可以尝试:
file_get_contents("http://domain/path/to/uri");
var_dump($http_response_header);
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.