Google's Text to Speech API : A PHP Wrapper Class

It’s really amazing how Google is providing cool APIs for every this and that! And here comes another one that impressed me
I am talking about the Text To Speech API by Google. It’s fantastic! I wrote a php wrapper class that would help you create mp3 files from texts
Of course, using Google as the medium!
Here’s the source code:
<?php
// FileName: tts.php
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}");
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}");
return $mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
And here’s demo :
<?php
require "tts.php";
$tts = new TextToSpeech("Hello World!");
$tts->saveToFile("filename.mp3");
?>


Here’s the source code:
<?php
// FileName: tts.php
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}");
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?q={$text}");
return $mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
And here’s demo :
<?php
require "tts.php";
$tts = new TextToSpeech("Hello World!");
$tts->saveToFile("filename.mp3");
?>