Ok, so recently I’ve been going through my bookmarks (mainly as an attempt to take my mind off various things…) and found a couple I had saved as ideas for projects. One of which required the use of JSON. Now, it may be popular it seems, but seriously, where are all the tutorials? Certainly not online thats for sure, atleast for what I was looking for anyway. To remedy this, I decided to piece together the few bits of code I could to give a basic example. This may be obvious to many, but when you are new to something and need a quick example, its a start. The following example is based on the code for a site API and as such needed to be altered to handle anything else. Enjoy.In this particular example, the source for the JSON itself is the tinyurl API.

<?php
$URL = "http://johntiernan.co.uk/";
$APIURL = "http://json-tinyurl.appspot.com/?url=".$URL;
$ch = curl_init($APIURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);
$data = json_decode($jsonData, true);
echo "Original: " . $URL . "
\n";
echo "New: " . $data['tinyurl'] . "\n";
?>

Labels: , , , , ,