今天写了一个微信公众开放平台看大家用的比较多的东西我就写了一个,第一个是中英日的翻译,第二个是天气查询的这两个开发起来其实很简单,我调用的都是百度的api,个人感觉百度的API是速度快的,而且准确率比较高,因为我们一般看网络通不通都是ping www.baidu.com 好了废话不多说了,直接开始了!上代码!

<?php
/**
 *  weixin.class.php 微信公众平台翻译和天气查询类
 *  
 * @copyright			widuu
 * @license				http://yun.widuu.com
 * @lastmodify			2013-5-31
 */
define("FANAPK", "zrGI089VlVeFiWSZNnKB0Ger");  //这个百度应用duapp.com创建一个应用获得的 API key
define("WAPK","CE8e67aca51b080896a27ec878cacf4a"); //这个key地址http://lbsyun.baidu.com/apiconsole/key?application=key来获取
//define your token
define("TOKEN", "xiaowei");
$wechatObj = new wechatCallbackapiTest();
$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, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
				$type = $postObj->MsgType;
				$event = $postObj->Event;
				$c = $postObj->Location_X;
				$d = $postObj->Location_Y;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";             
				if($type == "event" && $event == "subscribe")
                {
              		$msgType = "text";
                	$contentStr = "欢迎关注微度网络,支持英语翻译,天气查询等,查询翻译请直接输入,查询天气请输入您的地理位置如输入“天气+北京”等";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                	
                }else if($type == "text"){
					$keywords = explode("+",$keyword);
					if(isset($keywords[1])){
						$weather = $this->weather($keywords[1]);
						if(!empty($weather))
						{
							$data = $this->json2array($weather);
							foreach($data as $k =>$v)
							{
								$str.=$v['date'].'。天气:'.$v['weather'].'。风速:'.$v['wind'].'。温度:'.$v['temperature']."☺☺☺☺";
							}
						}else{
							$str="请您输入正确的地址,输入方式如天气+北京";
						}
						
						$msgType = "text";
						$contentStr = $str;
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
						echo $resultStr;
					}else{
						$fanyi = $this->fanyi($keyword);
						$msgType = "text";
						$contentStr = $fanyi;
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
						echo $resultStr;
					}
					
                }else if($type == 'location'){
					$location = $c.",".$d;
					$msgType = "text";
					$weather = $this->weather($location);
					$data = $this->json2array($weather);
					foreach($data as $k =>$v){
						$str.=$v['date'].'。天气:'.$v['weather'].'。风速:'.$v['wind'].'。温度:'.$v['temperature']."☺☺☺☺";
					}
					$msgType = "text";
					$contentStr = $str;
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
					echo $resultStr;
				}else{
					echo "您可以输入点什么";
				}

        }else {
        	echo "";
        	exit;
        }
    }
	//解析json为数组
	private function json2array($json) {
			if ($json) {
				foreach ((array)$json as $k=>$v) {
					$data[$k] = !is_string($v) ? $this->json2array($v) : $v;
				}
				return $data;
			}
		}	
	//获取天气信息
	private function weather($data)
	{
		//根据API调用输出数据
		$url="http://api.map.baidu.com/telematics/v2/weather?location={$data}&output=json&ak=".WAPK;
		$weather=$this->map_text($url);
		$data = json_decode($weather);
		return $data->results;
	}
	//调用翻译的api
	private function fanyi($data)
	{
		//根据API调用输出数据
		$url="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".FANAPK."&q={$data}&from=auto&to=auto";
		$fanyi=$this->map_text($url);
		$shuju=json_decode($fanyi);
		$result=$shuju->trans_result;
		return $result[0]->dst;
	}
	//解析地址的api
	private function map_text($url)  
	{
		if(!function_exists('file_get_contents')) {
		$file_contents = file_get_contents($url);
			} else {
			$ch = curl_init();
			$timeout = 5;
			curl_setopt ($ch, CURLOPT_URL, $url);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
			$file_contents = curl_exec($ch);
			curl_close($ch);
		}
			return $file_contents;
	}
	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;
		}
	}
}

?>
下边是我的公众平台做好了的,大家可以试试哈! 微信公众平台

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部