Tag Archives: php google api text to speech

php text to speech with google api

// Convert Words (text) to Speech (MP3)
// ————————————
$word= “Your text here”;
// Google Translate API cannot handle strings > 100 characters
$word = substr($word, 0, 100);

// Replace the non-alphanumeric characters
// The spaces in the sentence are replaced with the Plus symbol
$word = urlencode($word);

// If the MP3 file exists, do not create a new request
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, ‘;.$word);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the mp3 file
$mp3 = curl_exec($ch);

// close curl resource to free up system resources

file_put_contents(‘temp.mp3’, $mp3);

Leave a comment

Posted by on October 28, 2013 in Core PHP


Tags: ,