微信开发

<?php
	define("TOKEN","zhaohao");
	if(isset($_GET['echostr'])){
        //实现网址接入,调用valid函数
        valid();
    }
	else{
        responseMsg();
    	//调用响应消息函数
    }

	//验证消息
	function valid(){
    	if(checkSignature()){
            $echostr = $_GET['echostr'];
            if($echostr){
                echo $echostr;
                exit;
            }else{
        		echo "error";
                exit;
            }
        }
    }

	//检查签名
	function checkSignature(){
		$signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
        }else{
        	return false;
        }
    }

	//回应消息
	 function responseMsg(){
         //1.接受微信服务器传来的post
         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
         if(!$postStr){
             echo "error";
             exit;
         }

         //2.解析xml数据包
         $obj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
         
         //1)获得接收者的信息
         //$ToUserName=$obj->ToUserName;

         //1)获得发送者的信息
         //$FromUserName=$obj->FromUserName;

         //发送来的类型
         $MsgType=$obj->MsgType;
         switch($MsgType){
             case "event":
                 switch($obj->Event){
                     case "subscribe":
                         $reply="“欢迎关注我,谢谢”\r\n回复“新闻”,查看新闻\r\n也可当做简易计算器使用,例如输入“103+22”";
                         echo replyText($obj,$reply);
                     break;
                         
                     case "unsubscribe":
                         
                     break;
                 }
                 break;
                 
             case "text":
                 $content=$obj->Content;
                 preg_match('/(\d+)([+-])(\d+)/i',$content,$res);
                 if($res[2]=='+'){
                     echo jsq($obj,$content);
                 }
                 else{
                     switch($content){
                         case "新闻":
                             echo replyNews($obj);
                             break;
                             
                         case "点歌":
                             echo replyMusic($obj);
                             break;
                     }
                 }
             	 break;
                 
             default:
                 break;
         }
  		   
     }

	function replyText($obj,$reply){
        //preg_match('/(\d+)([+-])(\d+)/i',$content,$res);
        //if($res[2]=='+'){
        //    $result1=$res[1]+$res[3];
      //  }else if($res[2]=='-'){
       //     $result1=$res[1]-$res[3];
       // }
        $replyxml="<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[text]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                  </xml>";
        $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$reply);
        return $result;
    }
	//计算器
    function jsq($obj,$content){
        	preg_match('/(\d+)([+-])(\d+)/i',$content,$res);
            if($res[2]=='+'){
                $result1=$res[1]+$res[3];
            }else if($res[2]=='-'){
                $result1=$res[1]-$res[3];
            }
            $replyxml="<xml>
                         <ToUserName><![CDATA[%s]]></ToUserName>
                         <FromUserName><![CDATA[%s]]></FromUserName>
                         <CreateTime>%s</CreateTime>
                         <MsgType><![CDATA[text]]></MsgType>
                         <Content><![CDATA[%s]]></Content>
                      </xml>";
            $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$result1);
            return $result;
        }

	//新闻
	function replyNews($obj){
        $replyxml="<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[news]]></MsgType>
                    <ArticleCount>2</ArticleCount>
                    <Articles>
                    <item>
                    <Title><![CDATA[%s]]></Title> 
                    <Description><![CDATA[%s]]></Description>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                    <Url><![CDATA[%s]]></Url>
                    </item>
                    <item>
                    <Title><![CDATA[%s]]></Title>
                    <Description><![CDATA[%s]]></Description>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                    <Url><![CDATA[%s]]></Url>
                    </item>
                    </Articles>
                  </xml> ";
       	$title1="zyh";
        $title2="yxy";
        
        $description1="zyhde";
        $description2="yxyde";
        
        $picur1="http://1.zhaohao.applinzi.com/img/psb.jpg";
        $picur2="http://1.zhaohao.applinzi.com/img/timg.jpg";
        
        $url1="https://www.baidu.com/";
        $url2="https://www.baidu.com/";
        $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$title1,$description1,$picur1,$url1,$title2,$description2,$picur2,$url2);
        return $result;
    }
	
    function replyMusic($obj){
            $replyxml="<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[music]]></MsgType>
                        <Music>
                        <Title><![CDATA[%s]]></Title>
                        <Description><![CDATA[%s]]></Description>
                        <MusicUrl><![CDATA[%s]]></MusicUrl>
                        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
                        </Music>
                      </xml>";
            $title="勋章";

            $des="zyhde";

            $murl="http://1.zhaohao.applinzi.com/music/1.mp3";
       		
        	$hmurl="http://1.zhaohao.applinzi.com/music/1.mp3";
        
            $result=sprintf($replyxml,$obj->FromUserName,$obj->ToUserName,time(),$title,$des,$murl,$hmurl);
            return $result;
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值