simplexml_load_file 与 & Solr 的 url 中的(& 符号)

发布于 2024-12-10 15:57:45 字数 792 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2024-12-17 15:57:45

尝试:
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.php

Try:
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

若沐 2024-12-17 15:57:45

不起作用:

$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 and 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"));

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

需要注意的是,如果您传递带有空格的值,现在需要在添加之前对它们进行 urlencode 编码。例如:

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

$sort_insert = "&sort=price desc";

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

之后(有效):

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

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

Didn't work:

$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));

Manufacturer part of query came out as: "Bausch&Lomb";

Didn't work:

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

Adding spaces next to the words Bausch and Lomb produced simplexml_load file error.

Worked:

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

Swapping spaces for + works!

This is how I ended up doing it dynamically.

$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"));

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:

$sort_insert = "&sort=price desc";

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):

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

Thanks again... Back to the project!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文