- 论坛徽章:
- 18
|
本帖最后由 bikkuri 于 2015-06-04 08:28 编辑
大家好!
我有一个问题向大家请教。
我用PHP写了一个语音报时程序。- <?php
- header("Content-Type: text/html; charset=UTF-8");
- $cweekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
- $jweekday = array("日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日");
- $tweekday = array("Pazar","Pazartesi","Salı","Çarşamba","Perşembe","Cuma","Cumartes");
- $tmonth = array("Ocak","Şubat","Mart","Nisan","Mayıs","Haziran","Temmuz","Ağustos","Eylül","Ekim","Kasım","Aralık");
- $now = getdate(time());
- $cur_wday=$now['wday'];
- $cur_mon=$now['mon'] - 1;
- $cntime = date("现在时刻:Y年n月j日$cweekday[$cur_wday],G点i分。");
- $jptime = date("現在の時刻はY年n月j日$jweekday[$cur_wday]、G時i分です。");
- $trtime = "Şuan Tarıh " . date("j ") . $tmonth[$cur_mon] . date(" Y ") . $tweekday[$cur_wday] . ", " . date("G:i") . ".";
- $newfname = './tmp/now.wmv';
- echo $cntime;
- $creqBaseURL = 'http://translate.google.cn/translate_tts?ie=UTF-8&q=' . $cntime . '&tl=zh-CN';
- $cremote_file = fopen($creqBaseURL, "rb");
- if ($cremote_file){
- $newf = fopen($newfname, "wb");
- if ($newf){
- while(!feof($cremote_file)){
- fwrite($newf, fread($cremote_file, 1024 * 8),1024 * 8);
- }
- fclose($newf);
- }
- fclose($cremote_file);
- }
- echo "<br>";
- echo $jptime;
- $jreqBaseURL = 'http://translate.google.cn/translate_tts?ie=UTF-8&q=' . $jptime . '&tl=ja-JP';
- $jremote_file = fopen($jreqBaseURL, "rb");
- if ($jremote_file){
- $newf = fopen($newfname, "ab");
- if ($newf){
- while(!feof($jremote_file)){
- fwrite($newf, fread($jremote_file, 1024 * 8),1024 * 8);
- }
- fclose($newf);
- }
- fclose($jremote_file);
- }
- echo "<br>";
- echo $trtime;
- echo '<br><br>http://translate.google.cn/translate_tts?ie=UTF-8&q=' . $trtime . '&tl=tr-TR';
- $treqBaseURL = 'http://translate.google.cn/translate_tts?ie=UTF-8&q=' . "$trtime" . '&tl=tr-TR';
- $tremote_file = fopen($treqBaseURL, "rb");
- if ($tremote_file){
- $newf = fopen($newfname, "ab");
- if ($newf){
- while(!feof($tremote_file)){
- fwrite($newf, fread($tremote_file, 1024 * 8),1024 * 8);
- }
- fclose($newf);
- }
- fclose($tremote_file);
- }
- echo '<embed loop="1" autostart="true" hidden="true" src=./tmp/now.wmv />';
- ?>
复制代码 程序在执行的时候,中文和日文的报时都可以正常报出来。- 现在时刻:2015年6月2日星期二,21点09分。
- 現在の時刻は2015年6月2日火曜日、21時09分です。
- Şuan Tarıh 2 Haziran 2015 Salı, 21:09.
复制代码 但是土耳其语的部分则报错如下:- [Tue Jun 02 21:09:40 2015] [error] [client 180.170.45.231] PHP Warning: fopen(http://translate.google.cn/translate_tts?ie=UTF-8&q=\xc5\x9euan Tar\xc4\xb1h 2 Haziran 2015 Sal\xc4\xb1, 21:09.&tl=tr-TR): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request\r\n in /home/box/public_html/now.php on line 51
复制代码 看来是php解释器把土耳其语的TTS请求中的文本:- http://translate.google.cn/translate_tts?ie=UTF-8&q=Şuan Tarıh 2 Haziran 2015 Salı, 21:09.&tl=tr-TR
复制代码 不知道是什么原因,由UTF-8编码方式转换成了ASCII编码方式:- http://translate.google.cn/translate_tts?ie=UTF-8&q=\xc5\x9euan Tar\xc4\xb1h 2 Haziran 2015 Sal\xc4\xb1, 21:09.&tl=tr-TR
复制代码 所以出错。
前者贴到浏览器的地址栏是可以得到语音的,而后者就不行。- [box@ruizi:~]$ cat file|hexdump -C
- 00000000 c5 9e 75 61 6e 20 54 61 72 c4 b1 68 20 32 20 48 |..uan Tar..h 2 H|
- 00000010 61 7a 69 72 61 6e 20 32 30 31 35 20 53 61 6c c4 |aziran 2015 Sal.|
- 00000020 b1 2c 20 32 31 3a 31 38 2e 0a |., 21:18..|
- 0000002a
复制代码 大家可以看到有土耳其语字母Ş和ı,在UTF-8编码中分别用十六进制的\xc5\x9e和\xc4\xb1表示。
我试了把申请TTS的那条语句改写成:- $treqBaseURL = 'http://translate.google.cn/translate_tts?ie=UTF-8&q=' . iconv("ASCII", "UTF-8", "$trtime") . '&tl=tr-TR';
复制代码 但是还是报错:- [Tue Jun 02 23:34:04 2015] [error] [client 180.170.45.231] PHP Notice: iconv(): Detected an illegal character in input string in /home/box/public_html/now.php on line 50
- [Tue Jun 02 23:34:05 2015] [error] [client 180.170.45.231] PHP Warning: fopen(http://translate.google.cn/translate_tts?ie=UTF-8&q=&tl=tr-TR): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found\r\n in /home/box/public_html/now.php on line 51
复制代码 请问有什么办法让php解释器正确解读土耳其语的TTS请求呢?
而且让我觉得特别疑惑的是,中文和日文的TTS请求文本同样是UTF-8编码的啊?
为什么中文和日文的文本没有被转换成ASCII编码,执行没有报错呢?
谢谢大家! |
|