Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can i find a good video or tutorial on JSON with PHP? [closed]

Tags:

json

jquery

php

I have search the net many times and a lot of the JSON tutorials are too hard to understand. I am looking for JSON with jQuery and PHP. if anyone knows any videos or website i can look at that would be great

Thanks

like image 422
sam Avatar asked Aug 12 '10 20:08

sam


1 Answers

What is JSON and how do I create JSON objects?

The only thing you should know is that JSON is actually Javascript code. You can create arrays and objects with values like strings and numbers (and arrays and objects again)

  • You can store a lot of values in an array, seperated by a comma, like this:

    [12, "string", -30] // This is an array with 3 values

  • You can store a lot of values seperated by commas in an object too, WITH your own key, like this:

    {"key1": "value1", "key2": 234} // This is an object with two pair of keys and values

The fun thing is that you can use arrays and objects as values. So, when you put everything you learned above together, you can get JSON code like this:

{                  // Creates an object
    "key1": 12,    // with a key called "key1" and a number as value
    "key2": [      // and another key called "key2", with an new array as value
        10,        // the array has the number ten as first value
        "value"    // and a string containing "value" as its second value
    ]
}

How can I use JSON?

You can use JSON as a simple way to transfer data between the server and the client, like the Twitter API does, for example.

On the client side you can send and receive JSON data via Javascript and AJAX. Often people use jQuery as a library for this, because it has some build in JSON-validation stuff. On the server side you can use PHP for the job to convert the JSON data to a PHP object.

You can access values this way in Javascript:

someArray[0]; // gives you access to the first value of an array
someObject.key; // gives you access to the value of an object with key 'key'

Let me give you an example in which you'll open the flickr stream:

// $.getJSON(url, dataHandlerFunction); to get JSON-data
// Add the first image of the Flickr stream to the page
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", 
  function(data){

    // In this function you can do anything with the JSON code, because it's transformed into a Javascript object
    // If you open the above URL in your browser, you see that it exists of and object with
    // a key called items. Within is an array of objects, representing images
    // let's add the first one to our page
    var firstImg = data.items[0]; // First object, with image data.

    $("<img/>").attr("src", firstImg.media.m).appendTo('body');
  }
);

In PHP you can do almost the same:

// Get the JSON code form Flickr
$contents = file_get_contents('http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?'); 

// Decode it to a PHP object
$flickrStream = json_decode($contents);

// Display the image
echo '<img src="' . $flickrStream->items[0]->media->m . '" />';

Example: transfer data between client and server

You can use AJAX, as I said, to send data between the server and the client. Let me give you some simple example... You send something to the server, server sends something back.

// Sends a GET request to the server
$.ajax({
  url: '/to/your/php/file/',
  dataType: 'json',
  data: {
    "name": "client"
  },
  success: function(data){
    alert(data.messageFromServer); // alerts "Hi back, 
  }
});

PHP file (it's very unsafe this way, but it's a simple example)

<?php
  // A GET request was send, so you can use $_GET
  echo '{
          "messageFromServer": "Hi back, ' . $_GET['name'] . '"
        }';
?>
like image 170
Harmen Avatar answered Sep 28 '22 02:09

Harmen