Home  >  Article  >  Backend Development  >  PHP calls Google Translate program class

PHP calls Google Translate program class

不言
不言Original
2018-04-20 11:02:182876browse

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

<br/>
/*
 *Google翻译PHP接口
 */

$Google = new Google();
echo $Google->translate(&#39;中国&#39;, &#39;en&#39;);

class Google{

    public $out = "";
    public $google_translator_url = &#39;http://translate.google.com/translate_a/t&#39;;
    public $text = &#39;&#39;;
    public $from = &#39;&#39;;
    public $to = &#39;&#39;;
    
    function setText($text){ $this->text = $text;}
    function translate($text, $to = &#39;ru&#39;){
        $this->out  = "";
        $this->text = $text;
        $this->from = &#39;zh_CN&#39;;
        $this->to   = $to;        
        $gphtml = $this->postPage();	
        $arr = $this->decode($gphtml, 1);
        if(is_array($arr[&#39;sentences&#39;])){
            foreach ($arr[&#39;sentences&#39;] as $val){
                $this->out .= $val[&#39;trans&#39;];
            }
        }
        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($len<strlen($body))
                            $html.=substr($body,0,(strlen($body)-$len)-2);
                        else $html.=$body;
                        $bufferloop=FALSE;
                    }
                }else{
                    return  $this->decodeUnicode($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 = &#39;/".*?(?<!\\)"/&#39;;
        $string = preg_replace($match, &#39;&#39;, $json);
        $string = preg_replace(&#39;/[,:{}[]0-9.-+Eaeflnr-u nrt]/&#39;, &#39;&#39;, $string);
        if ($string != &#39;&#39;) { return null;}
        $s2m = array();
        $m2s = array();
        preg_match_all($match, $json, $m);
        foreach ($m[0] as $s) {
            $hash = &#39;"&#39; . md5($s) . &#39;"&#39;;
            $s2m[$s] = $hash;
            $m2s[$hash] = str_replace(&#39;$&#39;, &#39;$&#39;, $s);
        }
        $json = strtr($json, $s2m);
        $a = ($assoc) ? &#39;&#39; : &#39;(object) &#39;;
        $data = array(
            &#39;:&#39; => &#39;=>&#39;, 
            &#39;[&#39; => &#39;array(&#39;, 
            &#39;{&#39; => "{$a}array(", 
            &#39;]&#39; => &#39;)&#39;, 
            &#39;}&#39; => &#39;)&#39;
        );
        $json = strtr($json, $data);
        $json = preg_replace(&#39;~([s(,>])(-?)0~&#39;, &#39;$1$2&#39;, $json);
        $json = strtr($json, $m2s);
        $function = @create_function(&#39;&#39;, "return {$json};");
        $return = ($function) ? $function() : null;
        unset($s2m); 
        unset($m2s); 
        unset($function);
        return $return;
    }
    function decodeUnicode($str) {
        return preg_replace_callback(&#39;/\\u([0-9a-f]{4})/i&#39;,
                create_function(
                    &#39;$matches&#39;,
                    &#39;return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");&#39;
                ),
                $str);
    }
}

相关推荐:

PHP调用其他文件中的类    

PHP调用科大讯飞语音服务           


The above is the detailed content of PHP calls Google Translate program class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn