【发布时间】:2011-12-03 02:08:54
【问题描述】:
关于以下为什么不起作用的任何想法。
$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 被配置为禁止打开 url,所以 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;
}
【问题讨论】:
-
不确定是否应该在读取 URL 时将第二个参数设置为 true,因为它们不在 include_path 中。 this comment也可能和你的问题有关
-
1) JSON 无效。 2)由于某种原因,脚本无法访问该页面,例如您需要登录。3) 请向我们显示实际的 URL,以便我们计算出来。
-
除了lonesomeday,请尝试json_last_error()
标签: php json web-services file-get-contents