繁体   English   中英

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

[英]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);

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

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

$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);

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM