simplexml_load_file 与 & Solr 的 url 中的(& 符号)
我正在使用 Solr 并有以下查询,该查询在我的浏览器中运行良好:
http://www.someipaddress.com:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:"Bausch+%26+Lomb"
在返回 xml 的部分我看到:
<str>manufacturer:"Bausch & Lomb"</str>
但是,当我尝试使用 simplexml_load_file 获取上述 url 时,如下所示:
$xml = simplexml_load_file("http://127.0.0.1:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:\"Bausch+%26+Lomb\"");
我没有得到任何结果,因为 Solr 正在传递给制造商看起来像这样的字符串(来自 print_r):
[str] => Array ( [0] => shopid:40 [1] => manufacturer:"Bausch+%26+Lomb" )
因此,当我通过浏览器进行查询时,我传入 %26 但它在查询中正确处理它。但是当我使用 simplexml_load_file 时,它仍然为 %26,因此查询失败。
I am using Solr and have the following query which works fine from my browser:
http://www.someipaddress.com:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:"Bausch+%26+Lomb"
In part of the return xml I see:
<str>manufacturer:"Bausch & Lomb"</str>
However when I try and get the above url using simplexml_load_file like this:
$xml = simplexml_load_file("http://127.0.0.1:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:\"Bausch+%26+Lomb\"");
I get no results because Solr is being passed the manufacturer string that looks like this (from print_r):
[str] => Array ( [0] => shopid:40 [1] => manufacturer:"Bausch+%26+Lomb" )
So when I am doing the query through the browser I pass in %26 but it deals with it correctly in the query. But when I use simplexml_load_file it remains as %26 and so the query fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
simplexml_load_file(rawurlencode('http://127.0.0.1:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:"Bausch' .urlencode ('&'). 'Lomb"'))
请参阅注释
文件
参数:http://php.net /manual/en/function.simplexml-load-file.phpTry:
simplexml_load_file(rawurlencode('http://127.0.0.1:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18&fq=manufacturer:"Bausch' .urlencode('&'). 'Lomb"'))
See note on
file
parameter: http://php.net/manual/en/function.simplexml-load-file.php不起作用:
查询的制造商部分显示为:
“Bausch&Lomb”
;不起作用:
在 Bausch and Lomb 一词旁边添加空格会产生 simplexml_load 文件错误。
有效:
将空格替换为 + 有效!
这就是我最终动态地完成它的方式。
这适用于名称中带有“&”符号的制造商。
需要注意的是,如果您传递带有空格的值,现在需要在添加之前对它们进行 urlencode 编码。例如:
在我可以将其用于排序插入之前:
现在我需要对“price desc”进行 urlencode。当我尝试对整个 sort_insert 字符串进行 urlencode 时,simplexml 查询将失败。
之后(有效):
再次感谢...回到项目!
Didn't work:
Manufacturer part of query came out as:
"Bausch&Lomb"
;Didn't work:
Adding spaces next to the words Bausch and Lomb produced simplexml_load file error.
Worked:
Swapping spaces for + works!
This is how I ended up doing it dynamically.
That works for manufacturers with an ampersand in their name.
It is important to note that if you were passing values with spaces they will now need to be urlencoded before being added. For example:
Before I could just use this for my sort insert:
Now I need to urlencode just "price desc". When I tried to urlencode the whole sort_insert string the simplexml query would fail.
After (works):
Thanks again... Back to the project!