In this simple tutorial – We gonna convert Text into speech using PHP – So let’s check out:
In this method we gonna use Google Translate’s text to Speech API.
Note: This tutorial is for informational and Educational Purposes only. And it works for 100 Characters only.
Base URL: http://translate.google.com/translate_tts
It converts written words into audio. It accepts GET
requests.
q
The query string to convert to audio
tl
Translation language, for example, en-IN
for English (India), or en-us
for English(USA)
ie
Encoding format, use the default UTF-8
For Eg: https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q=Hello%20+%20How%20&tl=en-IN
Note: The value of the “q” parameter should have less than 100 characters else the Google Translate TTS API will throw an error.
So let’s start
– Open up your favorite code editor and create a form
<form method="post"><input id="txt" name="txt" type="textbox" /> <input name="submit" type="button" value="Convert to speech" /></form>
Here, we are using the POST method in form, as we gonna need it later on to fetch the submitted text value
Moving on to the next step :-
Let’s fetch the form value using PHP and find special characters in the value (if any) using ” htmlspecialchars “
<?php if(isset($_POST['txt'])){ $txt=$_POST['txt']; $txt=htmlspecialchars($txt); ?> <form method="post"><input id="txt" name="txt" type="textbox" /> <input name="submit" type="button" value="Convert to speech" /></form>
after this let’s encode the converted $txt using “rawurlencode” so, that it can be used in the API
<?php if(isset($_POST['txt'])){ $txt=$_POST['txt']; $txt=htmlspecialchars($txt); $txt=rawurlencode($txt); ?> <form method="post"><input id="txt" name="txt" type="textbox" /> <input name="submit" type="button" value="Convert to speech" /></form>
Now lets retrieve the data and convert it into audio using the Google Translate
<?php if(isset($_POST['txt'])){ $txt=$_POST['txt']; $txt=htmlspecialchars($txt); $txt=rawurlencode($txt); $html=file_get_contents('https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q='.$txt.'&tl=en-IN'); ?> <form method="post"><input id="txt" name="txt" type="textbox" /> <input name="submit" type="button" value="Convert to speech" /></form>
here “file_get_contents” will retrieve all the data from the url ( https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q=’.$txt.’&tl=en-IN) and will pass into $html
Now, after this we will convert all the data which we got from url into audio using <audio> </audio> tags and then echo out the player
<?php if(isset($_POST['txt'])){ $txt=$_POST['txt']; $txt=htmlspecialchars($txt); $txt=rawurlencode($txt); $html=file_get_contents('https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q='.$txt.'&tl=en-IN'); $player="<audio controls='controls' autoplay><source src='data:audio/mpeg;base64,".base64_encode($html)."'></audio>"; echo $player; ?> <form method="post"><input id="txt" name="txt" type="textbox" /> <input name="submit" type="button" value="Convert to speech" /></form>
here, we set audio to auto play and used base64 encoder in( $player=“<audio controls=’controls’ autoplay><source src=’data:audio/mpeg;base64,”.base64_encode($html).“‘></audio>”;)
and its all done.