php file_get_contents 给出错误

标签 php json http

当我在浏览器上运行以下查询时:

http://127.0.0.1:8096/solr/select/?q=june 17&start=0&rows=8&indent=on&hl=on&hl.fl=Data&wt=json

我得到了结果。没问题。注意 june 和 17 之间有一个空格

然而,当我在我的 PHP 中收到该查询时,即 $q=June 17 我使用

$url="http://127.0.0.1:8096/solr/select/?q=$q&start=0&rows=8&indent=on&hl=on&hl.fl=Data&wt=json";
$json_O=json_decode(file_get_contents($url),true);

在此之后,我在我的 Firebug 上看到了以下内容:

<b>Warning</b>:  file_get_contents(http://127.0.0.1:8096/solr/select/?q=june 17&amp;start=0&amp;rows=8&amp;indent=on&amp;hl=on&amp;hl.fl=Data&amp;wt=json) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported

但是请注意,如果我的查询词之间没有空格(我的意思是如果它是一个词)那么一切都是完美的。

为什么当我在浏览器上发出与 file_get_contents() 完全相同的查询时它工作正常。任何解决方案将不胜感激。

最佳答案

q 参数值中有一个空格。可能是这样。尝试 urlencode() 参数。

$url="http://127.0.0.1:8096/solr/select/?q=".urlencode($q)."&start=0&rows=8&indent=on&hl=on&hl.fl=Data&wt=json";

$json_O=json_decode(file_get_contents($url),true);

关于php file_get_contents 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10365510/

相关文章:

javascript - 将我首页的加载更多按钮添加到我的搜索页面

java - Gson.toJson( )"Exception java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits"

http - 在Kotlin Fuel中的http响应中看不到错误消息

python - 将请求放在 curl 中,但不放在 Python 中

php - 如何计算日期范围内的记录及其条件

phpbrew 安装特定 php 版本构建失败(make : *** [sapi/cli/php] ERROR 1)

php - 了解用于大型数据库/表的 MySql?

javascript - 动态加载系列 HighCharts

c++ - 从 JSON 数组获取天气

http - 如何发送和获取N> 10个URL的请求