开发者

file_get_contents() Google Text to Speech audio file non-english/latin characters

开发者 https://www.devze.com 2023-02-07 04:45 出处:网络
I searched through existing threads but couldn\'t find any solutions.. here\'s my question. I\'m using google\'s text to speech to download mp3 files. The function works great for english characters.

I searched through existing threads but couldn't find any solutions.. here's my question.

I'm using google's text to speech to download mp3 files. The function works great for english characters.. a开发者_Go百科nd languages using english characters. Unfortunately, for Russian (or any non-latin characters) my script generates the correct url, but doesnt download any content (but it does download a 0kb file.. empty)

file_put_contents($filepath,file_get_contents("http://translate.google.com/translate_tts?tl=".$lang."&q=".rawurlencode($transtext).""));

So a sample french word (phone) http://translate.google.com/translate_tts?tl=fr&q=t%C3%A9l%C3%A9phone

When I go to the page manually, and download the file, everything works.. but not with the file_get_contents.


You should add the input encoding parameter (ie=UTF-8) and encode the translation text accordingly.

For example, the following PHP snippet can be used to download the audio file for "téléphone" (in French) from the Google TTS service. Notice it does not work properly without the input encoding parameter.

$transtext = "t\xe9l\xe9phone"; // ISO-8859-1 string to be encoded in UTF-8
$base_url = 'http://translate.google.com/translate_tts?';
$qs = http_build_query(array(
    'tl' => 'fr',
    'ie' => 'UTF-8',
    'q' => utf8_encode($transtext)
));
$contents = file_get_contents($base_url . $qs);


This is a very very poor way to download a file through HTTP as you need to keep the whole file in memory. Either use cURL or fopen, both are simple. Ask if you need more help with either.

0

精彩评论

暂无评论...
验证码 换一张
取 消