google自动翻译API(PHP版)

google自动翻译API(PHP版) google翻译API

< ?php
$content="Halloween Sexy Pirate Captain Pink Costume";
$tran = new Google_translate_API();
echo $content.'
'; $tran->setValues($content); echo $tran->getValues().'
'; $content1="Make you the same as Terry Bogard in this The King Of Fighters costume for cosplay show."; echo $content1.'
'; $tran->setValues($content1).'
'; echo $tran->getValues(); ?> < ?php /** * Google翻译的API * @see 使用请参考最底部写法! * @editor Summer.Hu(blog:http://blog.hucde.com) */ class Google_translate_API { public $out = ""; public $text = ""; public $fromLanguage="en";//默认英文 public $toLanguage="fr";//默认法文 public $opts = array("text" => "", "language_pair" => ""); function setValues($text) { if($text != "") $fromLanguage=$this->fromLanguage; $toLanguage=$this->toLanguage; $this->opts["text"] = $text; if($fromLanguage != "" && $toLanguage != "") $this->opts["language_pair"] = $fromLanguage."|".$toLanguage; } function getValues() { $this->out = ""; if (strlen($this->opts['text']) > 500) { $str1 = substr($this->opts['text'],0,480); $str2 = substr($this->opts['text'],480,(strlen($this->opts['text']-480))); $google_translator_url1 = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($str1)."&langpair=".urlencode($this->opts['language_pair']).""; $google_translator_url2 = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($str2)."&langpair=".urlencode($this->opts['language_pair']).""; $response1 = $this->postPage(array("url" => $google_translator_url1)); $response2 = $this->postPage(array("url" => $google_translator_url2)); $resValues = $response1['responseData']['translatedText'].$response2['responseData']['translatedText']; } else { $google_translator_url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($this->opts['text'])."&langpair=".urlencode($this->opts['language_pair']).""; $response = $this->postPage(array("url" => $google_translator_url)); $resValues = $response['responseData']['translatedText']; } $this->out = $resValues; return $this->out; } function postPage($opts) { $response = ""; if($opts["url"] != "") { $ch = curl_init($opts["url"]); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if(curl_errno($ch)) $response = ""; curl_close($ch); $decoded = json_decode( $response, true ); } return $decoded; } } ?>

下载Google_translate_API.class.php