快捷搜索:   MySQL PHP linux 播放器

PHP 用谷歌API进行免费翻译

<?php
    $longtext = '学新通, Hello;
     
    function getTranslate($text){
        $entext = urlencode($text);
        $url = 'http://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl=zh-CN&q='.$entext;
        set_time_limit(0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS,20);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        curl_close($ch);
                $result = json_decode($result);
        if(!empty($result)){
            foreach($result[0] as $k){
                $v[] = $k[0];
            }
            return implode(" ", $v);
        }
    }
    echo getTranslate($longtext); // 学新通, 你好
?>



您可能还会对下面的文章感兴趣: