I am trying to implement methods discussed in this question to write a php function that downloads an audio file for a given string, but I can't seem to get around google's abuse protection. Results are sporadic, sometimes I get an audio file and other times it's an empty 2KB mp3 due to a response with "Our systems have detected unusual traffic from your computer network". Here is what I've got so far ( note the $file has a location in my code but for the purposes of this I've omitted it ) :
function downloadMP3( $url, $file ){
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_REFERER, 'http://translate.google.com/' );
curl_setopt( $curl, CURLOPT_USERAGENT, 'stagefright/1.2 (Linux;Android 5.0)' );
$output = curl_exec( $curl );
curl_close( $curl );
if( $output === false ) {
return false;
}
$fp = fopen( $file, 'wb' );
fwrite( $fp, $output );
fclose( $fp );
return true;
}
$word = "Test";
$file = md5( $word ) . '.mp3';
if ( !file_exists( $file ) ) {
$url = 'http://translate.google.com/translate_tts?q=' . $word . '&tl=en&client=t';
downloadMP3( $url, $file );
}
ie=UTF-8
in the query string. Try adding that, but I'll be back in a few hours in any case. – Schuhcurl
command on OSX at my university, and it works just fine. This makes me think that there's something wrong with your PHP code, or you're having network issues (maybe you're in a place that might spam Google a lot?). Unfortunately I have no knowledge of PHP nor how to run scripts on OSX or Ubuntu, so I can't really help debug your code... dostring
-type variables need to be in " characters instead of ' ? After a quick Google you might needCURLOPT_BINARYTRANSFER
as seen here. – Schuhcurl
command in my previous answer. It will work in any nix terminal (OSX, linux distros, etc), or install CURL for Windows. It's a one-line command:curl 'http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&client=t' -H 'Referer: http://translate.google.com/' -H 'User-Agent: stagefright/1.2 (Linux;Android 5.0)' > google_tts.mp3
. If the command doesn't work for you, it's definitely a network issue. If it *does work however, your PHP code needs some work. Probably a missing/misconfigured header. – Schuhcurl
in PHP is actually a bad option. PHP has anhttp_get
function. That is definitely a better solution than usingcurl
. You can also set HTTP headers with that function. I would try that! – Schuhclient=t
option, it downloads the mp3 file but it does not play. If I don't useclient=t
option, it still downloads the file but this time the file size is 0. Either way the file is not playable. I am doing this on windows. I wonder how they do this here: soundoftext.com – Chalutztk
parameter in the querystring). However, if you check the GET request from usingtranslate.google.com
, it generates a valid one that you can then use in a cURL command. Please see my edit to my answer on the other post which has the cURL working. You can add thetk
parameter to your PHP code and it should work. Your$url
should look like this now:$url = 'http://translate.google.com/translate_tts?q=' . $word . '&tl=en&tk=995126.592330&client=t';
– Schuh