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.
$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);
$data = json_decode($jsonData, true);
echo "Original: " . $URL . "
echo "New: " . $data['tinyurl'] . "\n";