Page 1 of 1

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

PostPosted: Thu May 31, 2012 10:48 pm
by silvester
It’s really amazing how Google is providing cool APIs for every this and that! And here comes another one that impressed me :D 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");
?>

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

PostPosted: Thu May 31, 2012 11:20 pm
by beniston
Thanks Silverster. it should be a useful weapon when its needed :D :D

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

PostPosted: Fri Jun 14, 2013 2:05 am
by beniston
Hi Silvester, now we also have a class from phpclasses site for the same.

http://www.phpclasses.org/package/8106- ... -text.html