PHP Program class for calling Google Translator

Source: Internet
Author: User
This article introduces the content of PHP call Google Translate program class, has a certain reference value, now share to everyone, the need for friends can refer to


/* *google Translate PHP interface */$Google = new Google (), Echo $Google->translate (' China ', ' 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 [' tra            NS '];    }} 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-&GT;TO}&AMP;CLIENT=JSON&AMP;IE=UTF-8&Amp;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, $la                Stlen);                } $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 = '/'. *?        <!\\) "/";        $string = Preg_replace ($match, ", $json);        $string = Preg_replace ('/[,:{}[]0-9.-+eaeflnr-u nrt]/', ', $string);        if ($string! = ") {return null;} $s 2m = Array ();        $m 2s = array ();        Preg_match_all ($match, $json, $m); foreach ($m [0] as $s) {$hash = ' ". MD5 ($s).            '"';            $s 2m[$s] = $hash;        $m 2s[$hash] = str_replace (' $ ', ' $ ', $s);        } $json = Strtr ($json, $s 2m); $a = ($ASSOC)?        ': ' (object) ';             $data = Array (': ' = ' = ' = ', ' [' = ' + ' array (', ' {' = ') ' {$a}array (",        ' ' = ') ', '} ' = ') ';        $json = Strtr ($json, $data); $json = Preg_replace (' ~ ([s (,];]) (-?)        0~ ', ' $1$2 ', $json);        $json = Strtr ($json, $m 2s);        $function = @create_function ("," "Return {$json};"); $return = ($function)?        $function (): null;         Unset ($s 2m);         unset ($m 2s);        Unset ($function);    return $return; } function Decodeunicode ($STR) {return Preg_replace_callback ('/\\u ([0-9a-f]{4})/I ', create_funct    Ion (' $matches ',                ' Return mb_convert_encoding (Pack ("h*", $matches [1]), "UTF-8", "ucs-2be");    ), $STR); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.