Working with JSON

A common use of JSON is to read data from a web server, and display the data in a web page.

Objects in PHP can be converted into JSON by using the PHP function json_encode() in reverse you can use json_decode()

To get POST JSON with PHP, you use the following Methods

1) file_get_contents

# Get JSON as a string
$json_str = file_get_contents('http://url');

# Get as an object
$json_obj = json_decode($json_str);

file_get_contents, this allows you to get JSON data from url.

2) cURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = curl_exec($ch);
$characters = json_decode($data, true);

Akhil Trivedi