simplexml_load_file with & (ampersand) in url with Solr

后端 未结 2 1750
醉梦人生
醉梦人生 2021-01-24 17:45

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&am         


        
相关标签:
2条回答
  • 2021-01-24 18:29

    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

    0 讨论(0)
  • 2021-01-24 18:42

    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!

    0 讨论(0)
提交回复
热议问题