当我在浏览器上运行以下查询时:
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&start=0&rows=8&indent=on&hl=on&hl.fl=Data&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/