Showing posts with label json decode php. Show all posts
Showing posts with label json decode php. Show all posts

Saturday, December 3, 2011

JSON - Javascript Object Notation

There were days when we used to create and use XML for transporting and store data and it was good at it. But I consider there was a problem with it getting data from it, I feel nausea. I used 'was' here because I also consider it as the way our daddies used to transfer data. Now is the day of JSON.

JSON is cool, easy, requires less memory(though we are not bothered). You can use it with any language and I love it. Frankly, I gave up XML before learning it because of JSON.

JSON looks like array we make in C\C++ or any other language. As we are familiar with the array, it makes understanding of JSON easy.

Syntax: {'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } }

Let's use it with PHP and Javascript.

PHP:
  1. $array = new array('data'=>array('J','S','O','N'), 'this'=>'json');
  2. $json = json_encode($array);
  3. $json = json_decode($json);
  4. echo $json->data[0];                             //output: J
  5. echo $json->this;                                   //output: json
Javascript:
  1. var json = {'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } };
  2. alert(json['data'][0]);                            //output: J
  3. alert(json->data[0]);                            //output: J
  4. alert(json->this);                                  //output: json
  5. alert(json['this']);                                  //output: json
We have studying here for PHP and Javscript as it is used mostly for web development. Go to json.org for better understanding and using with other languages.

Saturday, November 5, 2011

Zomato.com

Zomato.com is a new buzz on internet for foodies. They provide lists of restaurant through many filters like your location, by cuisine, by name(of course). Over it there are many more filters so you can choose the restaurant of your choice. They currently provide service in 10 major cities of India. You can download app for android, iphone, blackberry and nokia/symbian or you can visit at m.zomato.com on mobiles.

For website developers they have a search widget which can be embedded in site. Customize size of the widget according to the need of site and embed it using the script they provide.

From a point of view of web developer, content placement on the site is very good and the color combination of shades of dark red and shades of grey is also great. That is why the site looks cleaner.

Zomato has released their API publicly. Here we will talk about the API of zomato, not all but one of them. The API is very easy to use.

Requires:
Zomato API key, PHP, JSON

Using:
Let the key be 'api'. Now we would like to have names of cities where they provide service.
  1. <?php 

  2.  $cities = json_decode(get_file_contents("https://api.zomato.com/v1/cities.json?apikey=api"));
  3.  echo $cities->city[0]->city->name;
  4.  
  5. ?>
Explanation:

       3. 'apikey' is must in the url because it has to be parsed to get the JSON. The 'api' is to be replaced with
           you api key given by zomato. Function json_decode() will turn that JSON in a stdClass object.

       4. Data can be fetched from the object like the syntax in this line. Its similar to calling object variables or
           methods as done in class objects.

Note: SSL has to be enabled on your server, because look at the url it has 'https' which is secured.
         Respect the privacy of the users and read zomato policies before taking the application
         online.

So, go to zomato build your own application about restaurants, their menus and much more. This is a great site to find place where you will like to eat what you want to eat.