敝人想写一个翻译的程序,开始使用的是谷歌官方的翻译API,结果那个只支持GET方式的,有字数限制,字数一长久翻译不出来了,于是改转向post方式提交,找到谷歌开放api http://translate.google.com/translate_a/t这个可以用post方式提交数据,没有长度限制,可问题又来了,当我把英语翻译成西班牙语的时候就会显示一些乱码,同样法语德语也是一样的,我不知道是api的问题还是代码问题还是我的机器系统的问题,有没有大牛指点一二。谢谢~~附上代码如下<?php
header("Content-Type:text/html;charset=utf-8");
//英语->西班牙
$txt="This interface, when I tested on the content length is limited, but the speed was okay, not as good as the web version of the overall translation interface";
$sock = fsockopen("translate.google.com.hk", 80, $errno, $errstr, 30);  
if (!$sock) die("$errstr ($errno)\n");  
$data ="client=t&langpair=en|es&otf=1&pc=0&text=".$txt;  
fwrite($sock, "POST /translate_a/t HTTP/1.0\r\n");  
fwrite($sock, "Host: translate.google.com.hk\r\n");  
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
fwrite($sock, "Content-length: " .strlen($data) . "\r\n");  
fwrite($sock, "Accept: */*\r\n");  
fwrite($sock, "\r\n");  
fwrite($sock, "$data\r\n");  
fwrite($sock, "\r\n");  
$headers = "";  
while ($str = trim(fgets($sock)))  
$headers .= "$str\n";  
echo "\n";  
$body = "";  
while (!feof($sock))  
$body .= fgets($sock);  
preg_match('/\[\["(.*?)"/',$body,$arr);
echo $txt=$arr[1];
?>

解决方案 »

  1.   

    翻译出来的乱码例如Esta interfaz, cuando he probado en la longitud del contenido es limitado, pero la velocidad estaba bien, no tan buena como la versi\u00F3n web de la interfaz de traducci\u00F3n global
    \u00F3n会冒出这种东西
      

  2.   

    是欧洲字符ó,它的返回没问题,不过你要显示需要转成utf8, 或者用&#243;这样
      

  3.   


    请问我需要怎么做才能让他正确显示出来,编码已经是utf-8的了,谢谢
      

  4.   

    比较怪的方法,匹配出$arr[1]后
    $res    = json_decode('{"str":"' . $arr[1] . '"}', true);
    echo $res['str'];
      

  5.   


    不知道代码动了哪里,乱码又变了一种形式,变成这种的了。
    V�ase el blog de Ed Bott en este asunto. Vea tambi�n esta lista de ajustes mal.这个又该怎么办呢?代码如下<?php
    header("Content-Type:text/html;charset=utf-8");
    //英语->西班牙
    $txt="There is a lot of discussion of configuring Prefetch and the related subject boot defrag. The average PC user can ignore these subjects since Windows XP basically takes care of this area on its own. These configurations are part of what the utility Bootvis.exe discussed above was supposed to help manage. If you must tinker, do the RUNDLL32.exe command given above in the Bootvis.exe section. However, forget the often-mentioned idea of regularly deleting the contents of the  Windows Prefetch  folder. In fact, deleting Prefetch will initially slow down the boot until the folder is rebuilt. See Ed Bott's blog on the subject. Also see this bad tweaks list. ";
    $sock = fsockopen("translate.google.com", 80, $errno, $errstr, 90);  
    if (!$sock) die("$errstr ($errno)\n");  
    $data ="client=t&langpair=en|es&otf=1&pc=0&text=".$txt;  
    fwrite($sock, "POST /translate_a/t HTTP/1.0\r\n");  
    fwrite($sock, "Host: translate.google.com\r\n");  
    fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
    fwrite($sock, "Content-length: " .strlen($data) . "\r\n");  
    fwrite($sock, "Accept: */*\r\n");  
    fwrite($sock, "\r\n");  
    fwrite($sock, "$data\r\n");  
    fwrite($sock, "\r\n");  
    $headers = "";  
    while ($str = trim(fgets($sock)))  
    $headers .= "$str\n";  
    echo "\n";  
    $body = "";  
    while (!feof($sock))  
    $body .= fgets($sock);  
    preg_match_all('/\["(.*?)"/',$body,$arr);
    foreach ($arr[1] as $value) {  
        $txt.=$value;
    }  
    echo $txt."<hr>";
    ?>
      

  6.   

    出现方块是因为字体缺失,是因为你操作系统的locale不支持当前字体,假如你在控制面板里改成当前语言的system locale就可以显示了
      

  7.   

    $data ="client=t&langpair=en|es&otf=1&pc=0&text=".$txt;  改为:$data ="client=t&langpair=en|es&otf=1&pc=0&ie=UTF-8&oe=UTF-8&text=".$txt;  
      

  8.   

    附上部分代码<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php//英语-》德语
    $txt="There is a lot of discussion of configuring Prefetch and the related subject boot defrag. The average PC user can ignore these subjects since Windows XP basically takes care of this area on its own. These configurations are part of what the utility Bootvis.exe discussed above was supposed to help manage. If you must tinker, do the RUNDLL32.exe command given above in the Bootvis.exe section. However, forget the often-mentioned idea of regularly deleting the contents of the  Windows Prefetch  folder. In fact, deleting Prefetch will initially slow down the boot until the folder is rebuilt. See Ed Bott's blog on the subject. Also see this bad tweaks list. ";
    $sock = fsockopen("translate.google.com", 80, $errno, $errstr, 90);  
    if (!$sock) die("$errstr ($errno)\n");  
    $data ="client=t&langpair=en|es&otf=1&pc=0&ie=UTF-8&oe=UTF-8&text=".$txt; 
    fwrite($sock, "POST /translate_a/t HTTP/1.0\r\n");  
    fwrite($sock, "Host: translate.google.com\r\n");  
    fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
    fwrite($sock, "Content-length: " .strlen($data) . "\r\n");  
    fwrite($sock, "Accept: */*\r\n");  
    fwrite($sock, "\r\n");  
    fwrite($sock, "$data\r\n");  
    fwrite($sock, "\r\n");  
    $headers = "";  
    while ($str = trim(fgets($sock)))  
    $headers .= "$str\n";  
    echo "\n";  
    $body = "";  
    while (!feof($sock))  
    $body .= fgets($sock);  
    preg_match_all('/\["(.*?)"/',$body,$arr);
    $txt="";
    foreach ($arr[1] as $value) {  
        $txt.=$value;
    }  //德语-》英语
    $sock = fsockopen("translate.google.com", 80, $errno, $errstr, 30);  
    if (!$sock) die("$errstr ($errno)\n");  
    $data ="client=t&langpair=es|en&otf=1&pc=0&text=".$txt;  
    fwrite($sock, "POST /translate_a/t HTTP/1.0\r\n");  
    fwrite($sock, "Host: translate.google.com\r\n");  
    fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
    fwrite($sock, "Content-length: " .strlen($data) . "\r\n");  
    fwrite($sock, "Accept: */*\r\n");  
    fwrite($sock, "\r\n");  
    fwrite($sock, "$data\r\n");  
    fwrite($sock, "\r\n");  
    $headers = "";  
    while ($str = trim(fgets($sock)))  
    $headers .= "$str\n";  
    echo "\n";  
    $body = "";  
    while (!feof($sock))  
    $body .= fgets($sock);  
    preg_match_all('/\["(.*?)"/',$body,$arr);
    $txt="";
    foreach ($arr[1] as $value) {  
        $txt.=$value;
    }  
    echo $txt;
    exit;
    ?>
      

  9.   

    <!-- Google Translate Element -->
    <div id="google_translate_element" style="display:block"></div><script>
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: "af"}, "google_translate_element");
    };</script>
    <script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>