php json返回bool(false)

时间:2011-10-07 14:52:05

标签: php json web-services file-get-contents

关于为什么以下不起作用的任何想法。

$request_url = "someurl?myId=$id"; //returns feed like above

$json = file_get_contents($request_url, true); //getting the file content

$decode = json_decode($json, true);

var_dump($json);

当我将request_url粘贴到浏览器中时,我得到了json数据,但如果我在php中尝试,则var_dump只是bool(false);任何想法??

更新并修复 好的家伙感谢所有的帮助。你帮我跟踪了这​​个。事实证明,php.ini配置为禁止打开网址,因此file_get_contents将无法正常工作。我找到了各种网站上提到的以下方便功能,并使用它进行排序。这是我用来帮助别人的方法。

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

3 个答案:

答案 0 :(得分:0)

也许$ request_url的响应字符集不是'utf-8'或'ansi'。

答案 1 :(得分:0)

我复制了你的问题,当ajax url错误(未找到)时,我观察到bool(false)。 确保你的“someurl”有效。

如果JSON无效,您的脚本仍会正确打印。 如果网址没有返回任何内容,你仍然会看到'string(0)“”'

问题是无效的网址。

答案 2 :(得分:0)

根据PHP文档,file_get_contents在失败时返回false。这意味着该URL无效或无法从php访问。

如果您是从服务器尝试此操作,请确保您的防火墙没有阻止此传出流量。

相关问题