微信公众平台之综合模块

<?php

define("TOKEN", "wechatnow");//填写你的token
$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, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
				$j=$postObj->Location_X; 
				$w=$postObj->Location_Y; 
				$type=$postObj->MsgType;
//文字回复的xml文件				
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[text]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";
//图文信息
				$newsTpl="<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[news]]></MsgType>
							<ArticleCount>1</ArticleCount>
							 <Articles>
							 <item>
							 <Title><![CDATA[图文信息]]></Title> 
							 <Description><![CDATA[图文信息]]></Description>
							 <PicUrl><![CDATA[%s]]></PicUrl>
							 <Url><![CDATA[%s]]></Url>
							 </item>
							 </Articles>
							<FuncFlag>0</FuncFlag>
							</xml>";
//音乐信息
				$musicTpl="<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[music]]></MsgType>
							<Music>
							 <Title><![CDATA[TITLE]]></Title>
							 <Description><![CDATA[DESCRIPTION]]></Description>
							 <MusicUrl><![CDATA[http://jiekouphp.duapp.com/1.mp3]]></MusicUrl>
							 <HQMusicUrl><![CDATA[http://jiekouphp.duapp.com/1.mp3]]></HQMusicUrl>
 						    </Music>
							<FuncFlag>0</FuncFlag>
							</xml>";	
							
				switch ($type){
				case "event"://激发事件
				$contentStr = "感谢你的关注,回复1公司简介,2公司图片,3听音乐,发送手机图片看评价,发送位置信息看周边地图";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
               	break;
				case "text";//文字
				        switch($keyword){
						case "1";
						$contentStr = "我公司....";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						break;
						case "2";
						$a="http://jiekouphp.duapp.com/1.jpg";
						$b="http://www.baidu.com";
						$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,$a,$b);
						break;
						case "3";
						$resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time);
						break;
						default;
						$contentStr = "回复1公司简介,2公司图片,3听音乐,发送手机图片看评价,发送位置信息看周边地图";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						}
			  break;
			  case "image";//图片
			   $contentStr = "图片不错,80分";
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
				break;
			  case "location";
			   	$a="http://api.map.baidu.com/staticimage?width=640&height=320¢er={$w},{$j}&zoom=16";
				$b="http://api.map.baidu.com/marker?location={$w},{$j}&output=html";
				$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,$a,$b);
				break;
			    case "link";//链接
			    $contentStr = "感谢你的分享,不是病毒吧";
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
				break;
			  default;
			   	$contentStr = "这项功能还没开发";
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
			    
     }
  	echo $resultStr;
               


        }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;
		}
	}
}


?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值