Perform a solr response analysis in php and show them in the html table.

So, I have a lot of running solr instances, and to send a request, I just added a quick website. After I make the request URL, in php, I use the following to get the XML response from solr:

$solr_return= file_get_contents($full_request_URL); 

Now the answer is not in simple XML format, and it has solr-ness 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 look online, and there are many different ideas that make me think, maybe I'm completely off, and that’s not the way to do it. How would you do it if you were me?

The xml in $solr_return as follows:

 <?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> 

In this example, I want to show a table with three columns of 1, 2 and 3 and two rows test1, test2 and test3.

Thank you for your help in advance.

+4
source share
2 answers

The simplest thing is to add &wt=php to the Solr request URL and then run eval() and assign the results to the array (if you trust your Solr server not to send malicious code). Then Solr results will only be in a regular PHP array, and you can manipulate it as needed.

Alternatively, you can add &wt=json to 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.

+5
source

Try the following:

 <?php $solr_return = file_get_contents($full_request_URL); $solr_return = simplexml_load_string($solr_return); $xpath = $solr_return->xpath( '//result/doc' ); $rowCols = array(); if( count( $xpath ) > 0 ) { foreach( $xpath as $key => $value ) { if( !isset ( $rowCols[ $key ] ) ) { $rowCols[ $key ] = '<tr>'; } foreach( $value as $k => $v ) { $rowCols[ $key ] .= '<td>' . $v . '</td>'; } $rowCols[ $key ] .= '</tr>'; } } else { $rowCols[] = '<tr><td colspan="3"></td></tr>' } $rowCols = implode( '', $rowCols); ?> <html> <head></head> <body> <table border="0" width="50%" cellpadding="2" cellspacing-="3"> <tbody> <?php echo $rowCols;?> </tbody> </table> </body> </html> 

Hope this helps.

0
source

All Articles