1

我正在使用 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,因此查询失败。

4

2 回答 2

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"'))

请参阅file参数说明:http: //php.net/manual/en/function.simplexml-load-file.php

于 2011-10-18T12:03:10.133 回答
1

没用:

$url = 'http://127.0.0.1:8983/solr/select?q=*&fq=shopid:40&start=0&rows=18';
$url .= '&fq=manufacturer:"Bausch' .urlencode('&'). 'Lomb"';
simplexml_load_file(rawurlencode($url));

制造商部分查询结果为"Bausch&Lomb"

没用:

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"'))

在单词 Bausch 和 Lomb 旁边添加空格会产生 simplexml_load 文件错误。

工作过:

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"'))

为 + 交换空间!

这就是我最终动态完成它的方式。

$manufacturer = urlencode("Bausch & Lomb");
$manufacturer_insert = "&fq=manufacturer:\"$manufacturer\"";
$xml = simplexml_load_file(rawurlencode("http://127.0.0.1:8983/solr/select?q=$shopid_insert$start_insert$rows_insert$sort_insert$manufacturer_insert"));

这适用于名称中带有 & 符号的制造商。

重要的是要注意,如果您传递带有空格的值,则现在需要在添加之前对其进行 urlencoded。例如:

在我可以将它用于我的排序插入之前:

$sort_insert = "&sort=price desc";

现在我需要对“price desc”进行urlencode。当我尝试对整个 sort_insert 字符串进行 urlencode 时,simplexml 查询将失败。

之后(作品):

$sort = urlencode("price desc");
$sort_insert = "&sort=$sort";

再次感谢...回到项目!

于 2011-10-18T20:33:58.473 回答