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

Programming for Search Engines 101. An area for avid PHP and .NET developers to chat about Programming techniques and how to make better use of search engines.

Moderator: Moderators

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

Postby silvester » Thu May 31, 2012 10:48 pm

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");
?>
silvester
 
Posts: 89
Joined: Mon Nov 10, 2008 12:59 am

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

Postby beniston » Thu May 31, 2012 11:20 pm

Thanks Silverster. it should be a useful weapon when its needed :D :D
beniston
 
Posts: 502
Joined: Wed Nov 02, 2011 4:35 am
Location: Cochin
Tell us why you would like to become a WyseLabs Member:

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

Postby beniston » Fri Jun 14, 2013 2:05 am

Hi Silvester, now we also have a class from phpclasses site for the same.

http://www.phpclasses.org/package/8106- ... -text.html
beniston
 
Posts: 502
Joined: Wed Nov 02, 2011 4:35 am
Location: Cochin
Tell us why you would like to become a WyseLabs Member:


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest