2012-04-28 75 views
1

当我在浏览器中运行以下查询:PHP的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 

我得到的结果。没问题。请注意,有17

但六月间的空间,当我收到的查询在我的PHP即$ Q = 6月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); 

这个我看到了下面就我的萤火虫后:

<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()相比完全相同的查询时,它工作正常。任何解决方案将不胜感激。

回答

2

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); 
+0

谢谢Nadh。我使用的是urlencode,但可能不在正确的位置。您的代码段工作。万分感谢。 – 2012-04-28 17:06:19

1

这是因为file_get_contents函数发送到HTTP Web服务器,它看起来像这样

GET /solr/select/?q=bla HTTP/1.0 
Host: 127.0.0.1 
...[more headers here]... 

注意的请求,有一个在请求中指定的在HTTP版本( HTTP/1.0

现在,如果在你的请求字符串的空间,你送点什么样

GET /solr/select/?q=bla foo HTTP/1.0 
Host: 127.0.0.1 
...[more headers here]... 

您的服务器似乎解析foo作为版本并返回505 HTTP Version Not Supported。如果您编码字符串中的空格(例如,将其替换为%20,则不会发生这种情况)。