Search for restaurants using Zomato API in PHP

By Faruque Ahamed Mollick

Zonato is a popular website where we can search for restaurants in the various city. They provide their services in several countries in the world. On Zomato, you can see the information and review about various restaurants.

Well, as a developer, you can do many interesting apps using the API that Zomato provides. Let’s play with their API.

In this tutorial, we are going to build a simple restaurant search app using PHP programming language. To follow this tutorial, you need your Zomato API key which you can get from their website.

PHP Weather Forecast Script That Uses OpenWeatherMap API



Getting weather data from OpenWeatherMap API using PHP

Zomato sends us the search results by sending a request with our API key in a URL that looks like this – https://developers.zomato.com/api/v2.1/search?q={SEARCH_KEY}&start=0&count=10

We are going to show the first 10 search results. You can see that we have set the parameters to start from 0 and count to 10. That means we will get the first results.

We need to send our API key with header request in PHP curl. Also, we need to specify another header request application/json.

Below is a simple PHP code to get the JSON data for the search query pizza:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://developers.zomato.com/api/v2.1/search?q=pizza&start=0&count=10");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array(
  "Accept: application/json",
  "User-Key: f0baf53bd8c31d3c625e9d9c0d379379"
  );

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);


?>

After the above code if we want to see the  JSON formatted data with the code that you can see below:

echo "<pre>"; print_r($result); echo "</pre>";

But it is always easier to take the data from JSON after decoding it using PHP json_decode function just like you can see below:

<?php
   $json_data = json_decode($result);
   echo "<pre>"; print_r($json_data); echo "</pre>";
?>

Now we can see the array formatted data on the page. From the array, we can easily take each data. The array is a vast topic, so we are not going to discuss array here.

How to create JSON formatted text in PHP

Simple PHP app with restaurant search box using Zomato API

Here we are now going to build a simple PHP application where a search box is available. You can search for a restaurant by searching using the search box.

Let’s give the PHP file name as index.php and then put the below code inside it:

<form action="index.php" method="GET">
  <input type="text" name="zomquery"><input type="submit" name="zomsubmit" value="Search">
</form>

<?php

if (isset($_GET['zomsubmit'])) {
  $zomquery = $_GET['zomquery'];
  # Continue running code
}
else {
  //echo "Search key not found";
  exit();
}

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://developers.zomato.com/api/v2.1/search?q=".$zomquery."&start=0&count=10");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array(

  "Accept: application/json",
  "User-Key: f0baf53bd8c31d3c625e9d9c0d379379"
  );

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

$zomdata = json_decode($result);
$zomrestaurants = $zomdata->restaurants;

//echo "<pre>"; print_r($zomrestaurants); echo "</pre>";

    if (count($zomrestaurants) == 0) {
    	echo "<b>No results found for your search query</b>"; 
    	exit();
    }

 echo "Found: ".$zomdata->results_found." results<br/>";
 echo "Result shown: ".$zomdata->results_start." to ".$zomdata->results_shown."<br/><br/>";

foreach ($zomrestaurants as $restaurant) {
  echo "<h3>"[email protected]$restaurant->restaurant->name."</h3>";

    if ($restaurant->restaurant->thumb != "") {
    	echo "<img width='330' src='"[email protected]$restaurant->restaurant->thumb."' /><br/>";
    }
  
  echo "Restaurant ID: "[email protected]$restaurant->restaurant->id."<br/>";

  echo "User rating: "[email protected]$restaurant->restaurant->user_rating->rating_text."( "[email protected]$restaurant->restaurant->user_rating->aggregate_rating."/5 ) Depending upon "[email protected]$restaurant->restaurant->user_rating->votes." votes<br/>";

  echo @$restaurant->restaurant->location->address.", "[email protected]$restaurant->restaurant->location->city." <a href='"[email protected]$restaurant->restaurant->url."'>Visit restaurant page</a><br/>";

  echo "<br/><hr>";

  //echo "<pre>"; print_r($restaurant->restaurant); echo "</pre>";
}

?>

If you run the above code on a server, it will return you various informational data for the restaurant. Here we have used the previous URL that we have already discussed. You will see the restaurant name, restaurant id, user ratings, address, and URL. If you wish, then you can list more data from the array that you have got by decoding the JSON formatted data.

I hope you have understood this tutorial.

3 responses to “Search for restaurants using Zomato API in PHP”

  1. satyam kumar says:

    It was really a great help. I was not getting any positive help after searching the entire web.
    But this really helped me a lot.
    Thank you so much for such a good content.

    • Faruque Ahamed Mollick says:

      Your welcome. It’s my pleasure and I am also so happy to know that this tutorial for Zomato API helpful to you.

  2. anshulika says:

    Hi sir,
    Thanks so much,
    This code is really helpful for me but one problem comes actually I am using Zomato review API and the user review comes in short description instead of the whole description.

Leave a Reply

Your email address will not be published. Required fields are marked *