首頁  >  文章  >  後端開發  >  PHP呼叫Google翻譯的程式類

PHP呼叫Google翻譯的程式類

不言
不言原創
2018-04-20 11:02:182808瀏覽

这篇文章介绍的内容是关于PHP调用Google翻译的程序类,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下


/*
 *Google翻译PHP接口
 */

$Google = new Google();
echo $Google->translate('中国', 'en');

class Google{

    public $out = "";
    public $google_translator_url = 'http://translate.google.com/translate_a/t';
    public $text = '';
    public $from = '';
    public $to = '';
    
    function setText($text){ $this->text = $text;}
    function translate($text, $to = 'ru'){
        $this->out  = "";
        $this->text = $text;
        $this->from = 'zh_CN';
        $this->to   = $to;        
        $gphtml = $this->postPage();	
        $arr = $this->decode($gphtml, 1);
        if(is_array($arr['sentences'])){
            foreach ($arr['sentences'] as $val){
                $this->out .= $val['trans'];
            }
        }
        return $this->out;
    }
    
    function postPage(){
        $sockfd=socket_create(AF_INET,SOCK_STREAM,getprotobyname("tcp"));
        $enctext=urlencode($this->text);
        $post_fields="sl={$this->from}&tl={$this->to}&client=json&ie=UTF-8&oe=UTF-8&text={$enctext}";
        $post_content_length=strlen($post_fields); 
        $post_org="POST /translate_a/t HTTP/1.1rnHost: translate.google.comrnConnection: ClosernAccept-Encoding: gzip,deflate,sdchrnContent-Length: ".$post_content_length."rnrn".$post_fields;
        $ip=gethostbyname("translate.google.com");
        socket_connect($sockfd,$ip,80);
        socket_send($sockfd,$post_org,strlen($post_org),0);
        $buffer="";
        $html="";
        while(!strstr($buffer,"rnrn")){
            $buf="";
            $n=socket_recv($sockfd,$buf,2048,0);
            if($n!=0 && $n!=-1)
            {
                $buffer.=$buf;
            }           
        }
        $header=substr($buffer,0,strpos($buffer,"rnrn"));
        //echo $post_org."rn".$buffer;exit;
        if(!strstr($header,"chunked")){
            $html=substr($buffer,strpos($buffer,"rnrn")+4);
            while(1){
                $nrecv=socket_recv($sockfd,$buf,1024,0);
                if($nrecv!=0 && $nrecv!=-1){
                    $html.=$buf;
                }else{
                    socket_close($sockfd);
                    return  $this->decodeUnicode($html);
                }
            }
        }
        $html="";
        $body=substr($buffer,strpos($buffer,"rnrn")+4);
        $buf="";
        $lastlen=0;
        $recvloop=TRUE;
        $bufferloop=TRUE;
        $nRemainLen=0;
        while($recvloop){
            while($bufferloop){
                if($lastlen!=0){
                    $body=substr($body,$lastlen);
                }
                $pos=strpos($body,"rn");
                $len=hexdec(substr($body,0,$pos));
                if($len!=0){
                    $body=substr($body,$pos+2);
                    if(strlen($body)>$len+2){
                        $html.=substr($body,0,$len);
                        $body=substr($body,$len+$pos+4);
                        $lastlen=0;
                    }else{
                        $lastlen=$len+2-strlen($body);
                        if($lendecodeUnicode($html);
                }
            }
            $buf="";
            $nrecv=socket_recv($sockfd,$buf,1024,0);
            if($nrecv!=0 && $nrecv!=-1){
                $nRemainLen+=$nrecv;
            }
            if($nRemainLen>$lastlen) {
                $bufferloop=TRUE;
                $lastlen=$nrecv-($nRemainLen-$lastlen);
                $html.=substr($buf,0,$lastlen-2);
                $nRemainLen=0;
            }
            else $html.=$buf;
        }
    }
    function decode($json,$assoc = false){
        $match = '/".*?(? '=>', 
            '[' => 'array(', 
            '{' => "{$a}array(", 
            ']' => ')', 
            '}' => ')'
        );
        $json = strtr($json, $data);
        $json = preg_replace('~([s(,>])(-?)0~', '$1$2', $json);
        $json = strtr($json, $m2s);
        $function = @create_function('', "return {$json};");
        $return = ($function) ? $function() : null;
        unset($s2m); 
        unset($m2s); 
        unset($function);
        return $return;
    }
    function decodeUnicode($str) {
        return preg_replace_callback('/\\u([0-9a-f]{4})/i',
                create_function(
                    '$matches',
                    'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
                ),
                $str);
    }
}

相关推荐:

PHP调用其他文件中的类    

PHP调用科大讯飞语音服务           


以上是PHP呼叫Google翻譯的程式類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn