微信开发有道翻译接口代码

时间:2022-05-19 18:25:17 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
微市场微信导航今天给大家分享一个微信开发有道翻译接口的程序 /**

* wechat php test */

//define your token

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();

$wechatObj->responseMsg(); class wechatCallbackapiTest {

public function valid() {

$echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } }







public function responseMsg() {

//get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){

$postObj = simplexml_load_string($postStr, LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "



%s

0

'SimpleXMLElement',


"; if(!empty( $keyword )) {

$msgType = "text";

$contentStr = language($keyword);

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr; }else{

echo "Input something..."; } }else { echo ""; exit; } }

private function checkSignature() {

$signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];

$token = TOKEN;

$tmpArr = array($token, $timestamp, $nonce); sort($tmpArr);

$tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){ return true; }else{

return false; } } } ?>







function language($value){ $value = urlencode($value); $qurl = 'http://fanyi.youdao.com/openapi.do?keyfrom=""&key=""&type=data&doctype=json&ver


sion=1.1&q='.$value;

$f = new SaeFetchurl(); $content = $f->fetch($qurl);

$sina = json_decode($content,true); $errorcode = $sina['errorCode'];

$phonetic = $sina['basic']['phonetic']; $explains = $sina['basic']['explains']['0']; $interpret = $sina['basic']['explains']['1']; $interprets = $sina['basic']['explains']['2']; $trans = '';

if (isset($errorcode)){

switch ($errorcode){ case 0:

$trans = $sina['translation']['0']; break; case 20:

$trans = '要翻译的文本过长'; break; case 30:

$trans = '无法进行有效的翻译'; break; case 40:

$trans = '不支持的语言类型'; break; case 50:

$trans = '无效的key'; break; default:

$trans = '出现异常'; break; } } return

$trans."\n".$phonetic."\n".$explain."\n".$interpret."\n".$interprets; }



?>



http://pan.baidu.com/share/link?shareid=4276260206&uk=3607781403






本文来源:https://www.wddqw.com/doc/73ded3076bd97f192279e9f1.html