I know that many of the mac developers out there use PHP to develop their websites. That's why I wanted to show this technique for easily accessing the application profile of iusethis from PHP, using the same JSON data we used in a previous post to use iusethis data on the clientside.
Doing this kind of stuff on the server-side is prefered for several reasons. For one, you are not dependent on the end-user being able to run your javascript. Another advantage is search engines index this data much more easily than clientside generated markup. In addition, you can cache the data for a period, so that you don't need to poll the server so often.
However, for this simple example, let's just start by doing a http request to iusethis. (Note that in a real example, you would probably want to just silently ignore failed request, rather than throwing an error).
// Fetch json feed.
$request = 'http://osx.iusethis.com/json/firefox';
$response = file_get_contents($request);
if ($response === false) {
die('Request failed');
}
As you can see from this example, the json url is /json/
There are two separate implementations of JSON, a native PHP function, which is slow, but easy to install, and a C module, which is fast, but requires you to compile it into PHP as a module. We'll show you how to use both. First with the Library, PHP-JSON:
$data = json_decode($response);
That was easy, wasn't it? However, using the PHP version is just as easy, and as for the installation, you just need t drop the JSON.php file from the distribution into your PHP script dir.
require_once('JSON.php');
$json_service = new Services_JSON();
$data = $json_service->decode($response);
Now that we have the data, we can easily create something similar to the 'blog this' function from the application profile:
<img src="<?php echo $data[app][icon]?>"/>
<?php echo $data[app][name]?> used by
<?php echo $data[app][user_count]?> people</p>
Thanks to Cristobal Dabed for helping out with the actual PHP Code :)



Leave a comment