/*
*Google翻译PHP接口
*/$Google=newGoogle();echo $Google->translate('中国','en');classGoogle{public$out="";public$google_translator_url='http://translate.google.com/translate_a/t';public$text='';public$from='';public$to='';functionsetText($text){$this->text=$text;}functiontranslate($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;}functionpostPage(){$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;}}functiondecode($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;}functiondecodeUnicode($str){returnpreg_replace_callback('/\\u([0-9a-f]{4})/i',create_function('$matches','return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'),$str);}}