Friday, July 31, 2009

Google translate simple php client

This morning i were reading an english based blog. I came from italy and (as you can notice) im not the best english speller at all. Sometimes i need to use an online dictionary to translate words or sentences. One of the most dictionary translation is wordreference.com and also http://translate.google.com/.
Well im a web programmer, google offers a rich api to its services, so i thought to write a really simple php batch to translate words or simple sentence.

here a snippet:


<?

if($argc < 4) die("usage: gt.php \"sentence\" <start lang> <translation lang>");

$sentence = urlencode($argv[1]);
$start = $argv[2];
$arrive = $argv[3];

$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$sentence&langpair=$start%7C$arrive";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body);

if(!is_null($json->responseDetails)) { die($json->responseDetails); }

echo $json->responseData->translatedText;

Hope someone could find this useful. You can call it as the follow:
gt.php "sentence or words" <strat> <translate>

Have fun =) !

Thursday, July 9, 2009

Directory listing in PHP

[edit] i write a new function: PHP directory listing : convert a folder tree into an array [/edit]

Hello world!

Lately im working on a series of tools which can be useful to organize works in web. Of course, nothing new, but im developing somthing mine and small that i can overwork instead of more complex tools.

This run for web of course and my interest is about:
  • deploy
  • backup
  • maintenance
Well about deploy i actually don't know what i want.
Backup it's what im working at.
maintenance i mean a simple tool that switch web-sites/single page in maintenance or work.

About Backup:
I first thought at a tool for a full and incremental backup, but in the end i decided to add the possibility to manage backup as project, saving status about multiple projects. Im currently working and the first piece of code i will give you is a pair of functions that help you to list directories recursively (and turn it into a PHP array.

function create_tree ($path) {
if(is_dir($path)) {
$tree["$path"] = array();
$tree = get_tree($path, $tree);
return $tree;
} else {
echo "<$path> is not a directory";
exit;
}
}

function get_tree($path, $tree) {
$dir = dir($path);
while(false !== ($entry = $dir->read())) {
$complete = "$path/$entry";
if(is_dir($complete) && $entry != "." && $entry != "..") {
$tree["$path"]["$entry"] = get_tree($complete, $tree[$path][$entry]);
} elseif($entry != "." && $entry != "..") {
$mod = date('d-m-Y H:i:s', filemtime($complete));
$tree[] = array( "fn" => $entry, "mod" => $mod);
}
}
return $tree;
}
And that is that, as you can see when it adds a new entry into the array it save last modification time too, sure will be useful to incremental backup.

TODO: pretty print!!!!

Tuesday, July 7, 2009

it's a rainy day

Milan.. a rainy day.. what happen???

KERNEL PANIC!