How to parse Azure Search response in PHP?

699 Views Asked by At

Currently I try Azure Search.

I managed to insert documents in the index, and now I want to parse my result.

My code looks like this:

<?php
$url ="";
   $url .="https://mywebsite.search.windows.net/indexes/test/docs";
   $url .= "?search=";
   $url .= $keyword;
   $url .= "&api-version=2014-07-31-Preview";
   print $url;


   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,$url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_TIMEOUT, 60);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
           'api-key: mytoken',
           'Accept: application/json',
     ));

   $data = curl_exec($ch);

   if (curl_errno($ch)) {
   print "Error: " . curl_error($ch);
   } else 
   {
   // Show me the result
   print var_dump($data);
   curl_close($ch);
   }
?>

My program works fine, but I can't parse my result who look like this:

string(633) "{"@odata.context":"https://mywebsite.search.windows.net/indexes('adventurehotel')/$metadata#docs(hotelId,baseRate,description,hotelName,category,tags,parkingIncluded,smokingAllowed,lastRenovationDate,rating,location)","value":[{"@search.score":0.16137227,"hotelId":"1","baseRate":199.0,"description":"Best hotel in town","hotelName":"Fancy Stay","category":"Luxury","tags":["pool","view","wifi","concierge"],"parkingIncluded":false,"smokingAllowed":false,"lastRenovationDate":"2010-06-27T00:00:00Z","rating":5,"location":{"type":"Point","coordinates":[-122.131577,47.678581],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}}}]}"

Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

Just json_decode it, it is just a json string.

json_decode, if called with true as the second parameter, will give you an associative array representation of the string you want to parse. The string itself should be a valid json string, or you will get null as the function result.

Take note, that as of php 5.5 and higher, json extension should be installed separately of the main php install due to its license.