使用Google Text-To-Speech API保存语音音频


Using Google Text-To-Speech API to save speech audio

我正在尝试实现本问题中讨论的方法,以编写一个php函数,下载给定字符串的音频文件,但我似乎无法绕过谷歌的滥用保护。结果是零星的,有时我会得到一个音频文件,有时它是一个空的2KB mp3,因为响应是"我们的系统检测到来自您计算机网络的异常流量"。以下是我到目前为止得到的内容(请注意,$文件在我的代码中有一个位置,但出于此目的,我省略了它):

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 );
}

尝试另一项服务,我刚刚发现一项比谷歌翻译更好的服务;谷歌文本-语音API