程式碼如下:
<?php
//http get query參數設定 tl設定國家語言(ex:en、zh_TW) q設定要語音的 內容
$qs = http_build_query(array("ie" => "utf-8","tl" => 'zh_TW', "q" => '你好'));
$ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n")));
$soundfile = file_get_contents("http://translate.google.com/translate_tts?".$qs, false, $ctx);
//設定檔頭為影音檔
header("Content-type: audio/mpeg");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
echo($soundfile);
?>