重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

微信公众平台开发--谷歌翻译

时间:2020-09-05 02:30:08 作者:重庆seo小潘 来源:
1)Google翻译接口2)微信调用3)效果展示----------------------------------------------------------------------------------------1)Google翻译接口Google提供翻译的API接口,参见https://developers.google.com/translate/v2/getting_startedbutGoogle

1)Google翻译接口2)微信调用3)效果展示----------------------------------------------------------------------------------------1)Google翻译接口Google提供翻译的API接口,参见https://developers.google.com/translate/v2/getting_startedbutGoogle Translate API is a paid service.so想利用Google Translate API free charge 就要另外想办法了Google提供免费的在线翻译功能,因此,可以通过Web发送翻译请求给Google,接收它的html返回,然后,通过分析html获取翻译后的文字.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;}}更有甚者,有人发现通过http://translate.google.com/translate_a/t?client=p与Google交互可以得到json返回,这就相当于API使用了 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;}Google翻译接口示例: http://download.csdn.net/detail/d_eng/6563915这里要注意的有两个问题1)编码问题,例子中都采用utf-8,连php文件的属性都是utf-82)google问题,google在中国不保证一直能连上,虽然有多个链接 google.com/google.cn/google.com.hk2)微信调用有了接口,在微信中调用就简单了接口文件 translate_func.php (save as utf-8)<?php /*2 Google Translate interface1)Google Translate WEB IFget translated text by parsing return html which code is GBKfunction translate_web()2)Google Translate JSON IFget translated text which format=json(set client<>t)&default code=GBKfunction translate_json() d_eng (sh109419@163.com) 2013-11-16*/ /*Google Translate WEB IFget 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;}functionwith_chinese($text){returnpreg_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)!