Parse solr response in php and show them in a html table

html, php, solr, xml

Solution

The easiest thing to do is to add `&wt=php` to the Solr request URL, and then run `eval()` and assign the results to an array (assuming you trust your Solr server not to send malicious code back). Then the Solr results will just be in a regular PHP array and you can manipulate it as you need.

Alternatively you can add `&wt=json` at the end, get the results in JSON format and then run `json_decode()` on the returned string in PHP to convert it to a PHP array.

Problem

So I have many solr instances running and to submit a search I just put a quick website together. After I make the request URL, in php I use the following to get the XML response back from solr: ``` $solr_return= file_get_contents($full_request_URL); ``` Now the response is not in a simple xml format and it has solr-ness to it if you know what I mean. I want to be able to parse the returned xml and show them in rows in a table in html. I've been looking online and there are many different ideas that make me think maybe I'm totally off and this is not the way to do it. How would you do this if you were me? The xml in `$solr_return` looks like this: ``` <?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">95</int> <lst name="params"> <str name="indent">true</str> <str name="rows">10</str> <str name="start">0</str> <str name="q">*:*</str> <str name="shards">some shards here</str> </lst> </lst> <result name="response" numFound="2403043" start="0"> <doc> <str name="1">test1</str> <str name="2">test2</str> <str name="3">test3</str> </doc> <doc> <str name="1">test1</str> <str name="2">test2</str> <str name="3">test3</str> </doc> </result> </response> ``` For this example, I want to show a table with three columns of 1, 2, and 3 and two rows of test1, test2, and test3. Thank you for the help in advance.

Original source