Using Google Text to Speech API - PHP

U
Published in: PHP

Google offers an undocumented text to speech API that help you transform text into voice. You can see a live demo at ctrlq.org/listen.

The following snippet of PHP code is responsible for the conversion while the MP3 files are cached to reduce requests to Google Translation servers.

<?php

// Convert Words (text) to Speech (MP3)
// ------------------------------------

// Google Translate API cannot handle strings > 100 characters
   $words = substr($_GET['words'], 0, 100);

// Replace the non-alphanumeric characters
// The spaces in the sentence are replaced with the Plus symbol
   $words = urlencode($_GET['words']);

// Name of the MP3 file generated using the MD5 hash
   $file  = md5($words);

// Save the MP3 file in this folder with the .mp3 extension
   $file = "audio/" . $file . ".mp3";

// If the MP3 file exists, do not create a new request
   if (!file_exists($file)) {
     $mp3 = file_get_contents(
        'http://translate.google.com/translate_tts?q=' . $words;
     file_put_contents($file, $mp3);
   }
?>

Embed the MP3 file using the AUDIO tag of HTML5.

<audio controls="controls" autoplay="autoplay">
  <source src="<? echo $file; ?>" type="audio/mp3" />
</audio>

Google Text to Speech - Command Line

You can also convert any piece of text to MP3 files from the command line using the wget or CURL command. Remember that the value of “q” parameter should have less than 100 characters else the Google Translate TTS API will throw an error.

wget -q -U Mozilla -O audio.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world
curl -A "Mozilla" "http://translate.google.com/translate_tts?tl=en&q=hello+world" > audio.mp3
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: PHP

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube