simplexml_load_file with & (ampersand) in url with Solr

给你一囗甜甜゛ 提交于 2019-12-02 05:52:32

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

someuser

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!