Sep
27

JSON Datadata and PHP

I have noticed with the Yahoo YUI! Javascript library use JSON Data alot for there Datasources. Now Turning your PHP Array into a JSON Array is very easy namely if you use PHP 5.2+ but even if not there are some easy to get Librarys out there that can convert your array in no time and you don’t need full root access to your server to do it. In this article I will only explain how to do it in the *nix Systems as that is all that I use. But if you look around enough i’m sure you can find some information for windows too.

PHP 5.2


In PHP 5.2 The JSON Library comes bundled with PHP so its very easy and extremely fast. here is a example of how you would use these new functions

 
$arr = array(url => 'url here',
		    name=>'name',	
		  );
 
echo '{"sample":'.json_encode($arr).'}';

PHP Below 5.2 Without Root Access


Below 5.2 without root access, the way to go is thru the PERL librarys here is the link

include_once('JSON.php');
$json = new Services_JSON();
 
$arr = array(url => 'url here',
		    name=>'name',	
		  );
 
echo '{"sample":'.json_encode($arr).'}';

PHP Below 5.2 With Root Access


For those of you that need to keep your current version of PHP but want the speed gained by using the PHP JSON extension can download and install it yourself, granted you have root access to the box your on. The installation is very simple, just follow the directions on the web site and you can have this extension installed and working in 10 minutes.

http://www.aurore.net/projects/php-json/

This way you can use the native PHP JSON functions, so the example code for PHP 5.2.0 or higher will work.

Jul
29

Conky

For a system monitor on my Ubuntu systems, I have found Conky is a excellent application. It is Script-able so you can make it look how ever you want, and if that isn’t enough it can execeute shell scripts and output the results to your desktop… I’ve seen a lot of people make Ruby scripts to extend conky but since I don’t know Ruby very well I’ve decided to completely misuse PHP and extend my Conky with PHP scripts! In this post, I will Show my Default Conky Appearence and post my .conkyrc file and the scripts I’ve used to make it look this way and explain what they do.

First Install Conky so type this into a Terminal

sudo apt-get install conky

To use the PHP scripts from conky, make sure you install PHP and PHP-CLI which PHP-CLI is pre-packed with PHP since v4.3

Even thou I use GNOME I love Amarok music player, so I use it even thou it requires KDE library files, For this script to work you will require the DCOP-PHP Class which I am hosting here. Please Note : I Did not write this Class, I am just hosting it here, if there is a problem with it please leave a comment below and I will remove it.

Personally I created a PHP directory inside my home directory, this is where i put all my classes and php files for conky to use.

Save this file in your newly created PHP directory, I called it np.php but thats up to you. The following file is a copy and general cleanup of the example script that came with the dcop.php class files. I made it work for amarok and cleaned it up some to save on server resources.


#!/usr/bin/php	// The ever important PHP shebang line
<?php
require("/home/shawnc/php/dcop.class.php");  // change this to the directory you saved the dcop.class.php file to
 
$dcop = new dcop;
$dcop->application("amarok","",""); //1st argument is necessary, 2nd and 3rd are either specified in your class
//or the defaults will be used
 
$dcop->app_function("player","isPlaying"); //decide if amarok is even playing
$isplay = implode(" ",$dcop->call());
 
if ($isplay) // If amarok is playing then keep going, if not don't waste server resources for nothing.
{
 
	$dcop->app_function("player","nowPlaying"); //call amarok's player function and get nowPlaying as an argument
	$playingstring = implode(" ",$dcop->call()); //This should return what's currently playing on juk
 
	$dcop->app_function("player","album"); //gimme the album
	$playingalbum = implode(" ",$dcop->call());
 
	$dcop->app_function("player","totalTime"); //how much is the total time (in seconds)
	$playing_tot_time = implode(" ",$dcop->call());
 
	$dcop->app_function("player","currentTime"); //how much is the current time (in seconds)
	$playing_cur_time = implode(" ",$dcop->call());
 
	echo "Currently playing in Amarok: \n" . $playingstring . "\n from the album: " . $playingalbum  . "\n";
	echo "Currently " . $playing_cur_time . " seconds far in the " . $playing_tot_time . " seconds long song\n\n";
}
 
?>
top
Close
E-mail It