今天又去论坛逛了逛,论坛真的是个好地方,又搜刮了一些好东西。拿出来给大家分享一下。这是个谷歌的英文翻译接口,用的是curl,因为目前我对于curl还没有深入研究,就不献丑解释了,上代码。需要注意的一点就是编码问题,他默认返回的是json格式的gbk编码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php class Google_API_translator { public $out = ""; function translate($text) { $this->out = ""; $text = urlencode($text);//要翻译的单词 $google_translator_url = "http://translate.google.com/translate_a/t?&client=t&text=".$text."&sl=en&tl=zh_CN"; $gphtml = $this->postPage(array("url" => $google_translator_url)); $this->out = $gphtml; return $this->out; } function postPage($opts) { $html = ""; if($opts["url"] != "") { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $opts["url"]); $html = curl_exec($ch); if(curl_errno($ch)) $html = ""; curl_close ($ch); } return $html; } } $g = new Google_API_translator(); $a = json_decode($g->translate("student")); ?> |
本文链接:http://www.521php.com/archives/522/
程序本天成,妙手偶得之!我们只是代码的搬运工!
转载请注明:http://www.521php.com/archives/522/