t)&default code=GBK function translate_json() d_eng (sh109419@163.com) 2013-11-16 */ /* Google Translate WEB IF get translated text by parsing return html which code is GBK */ //header("Content-Type:text/html; charset=utf-8"); function translate_web($text, $language="auto|en") { if (empty($text)) return false; $url = "http://google.cn/translate_t?ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $html=file_get_contents($url); // parse html // html souce: TTS_TEXT_SIZE_LIMIT=100;TRANSLATED_TEXT='世界,你好!';INPUT_TOOL_PATH='//www.google.com'; $mode= ("/TRANSLATED_TEXT='(.*)';INPUT_TOOL_PATH/"); if (preg_match($mode,$html,$out)){ return $out[1];//ret; }} function translate_json($text, $language="auto|en") { if (empty($text)) return false; $url = "http://translate.google.cn/translate_a/t?client=p&ie=UTF-8&oe=UTF-8&langpair=".$language."&text=".urlencode($text); $json=file_get_contents($url); $data = json_decode($json); return $data->sentences[0]->trans; }function with_chinese($text){ return preg_match('/[x7f-xff]/',$text);} function translate($text) { if (with_chinese($text)) { return translate_json($text,'zh-CN|en'); } else { return translate_json($text,'en|zh-CN'); }}?>
调用代码片段
if ($RX_TYPE=="text") { include("translate_func.php"); $resultStr = $this->responseText($postObj, translate(trim($postObj->Content))); }
3)效果展示
以上就是微信公众平台开发--谷歌翻译的内容,更多相关内容请关注PHP中文网(www.php.cn)!