RSS

Tag Archives: text-to-speech using php

php text to speech with google api

<?php
// 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, ‘http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=&#8217;.$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
curl_close($ch);

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

Advertisements
 
Leave a comment

Posted by on October 28, 2013 in Core PHP

 

Tags: ,