基于zhphpWeixinApi.class.php开发微信+ 百度API翻译案例

<?php
header('Content-type: text/html; charset=utf-8');#设置头信息
require_once('zhphpWeixinApi.class.php');#加载微信接口类文件
$zhwx=new zhphpWeixinApi();//实例化
$configArr=array('token'=>'wxtest1','appid'=>'wx5b79a26868798826',
'appSecret'=>'a95952819cc768fc130bc0cc0444ae77','myweixinId'=>'gh_746ed5c6a58b');
$zhwx->setConfig($configArr);//配置文件
if($zhwx->weixinBaseApiMessage()){
    $keyword=$zhwx->getUserTextRequest();//得到用户发的微信内容
    $msgtype=$zhwx->getUserMsgType();//得到用户发的微信数据类型
    $SendTime=$zhwx->getUserSendTime();//得到用户发送的时间
      //如果 keyword 为非空的情况下
    if( ! empty($keyword)){
		#由于我们这里是案例,所以规定: 程序员@英文,在实际开发场景中是获取表单值
		 $data=explode('@',$keyword);
		 $yuyanArray=array( '中文'=>'zh', '英文'=>'en', '粤语'=>'yue','日语'=>'jp', '韩语'=>'kor',
		 '泰语'=>'th','俄语'=>'ru');
		  $content='';
		 if(array_key_exists($data[1],$yuyanArray)){
			 ##百度翻译 api 参数
			  $appID='20160218000012508';
			  $secKey='64wObt8yQWCe7bGMGEDO';
			  $salt=rand(10000,99999);
			  $query=$data[0];
			  $sign=md5($appID . $query . $salt . $secKey);
			  $to=$yuyanArray[$data[1]];
			  ##url
              $url='http://api.fanyi.baidu.com/api/trans/vip/translate?';
			  $url.='q='.urlencode($query).'&from=auto&to='.$to.'&appid='.$appID.'&salt='.$salt.'&sign='.$sign;
			  $jsonData=$zhwx->curl_post_https($url);
			  $fanyijieguo=json_decode($jsonData,true);
			  $fy=$fanyijieguo['trans_result'];
			  $content='源词语:'.$fy['0']['src']."\n";$content.='翻译:'.$fy['0']['dst']."\n";
			  $content.='zhphp微信平台翻译,希望对你有帮助';
              $zhwx->responseMessage('text',$content);
			}else{
			 $content='暂时未提供'.$data[1].'翻译功能';
			 $zhwx->responseMessage('text',$content); 
		 }
	}
 }else{
		   echo '配置文件失败';
 }

发布了186 篇原创文章 · 获赞 24 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/echocdzh/article/details/50699743