Google's Text to Speech API : A PHP Wrapper Class
Monday, December 14th, 2009It’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 /* * A PHP Class that converts Text into Speech using Google's Text to Speech API * * Author: * Abu Ashraf Masnun * http://masnun.com * */ 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(); $tts->setText("Hello World!"); $tts->saveToFile("masnun.mp3"); ?>
You can alternatively pass the text to the constructor of the object like this:
<?php require "tts.php"; $tts = new TextToSpeech("Hello World!"); $tts->saveToFile("masnun.mp3"); ?>
That is simple, isn’t that? Hope you like it!
[...] This post was Twitted by masnun [...]
[...] This post was mentioned on Twitter by Abu Ashraf Masnun, jimmayes. jimmayes said: RT @masnun: Using Google's Text To Speech API with PHP: http://bit.ly/8FSuO1 #Google #PHP [...]
[...] See the original post: Google's Text to Speech API : A PHP Wrapper Class | maSnun.com [...]
Nifty nice class… carry on digging google