订阅号功能----音乐

<?php
/**
  * 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, 'SimpleXMLElement', LIBXML_NOCDATA);
   $fromUsername = $postObj->FromUserName;
   $toUsername = $postObj->ToUserName;
            //$type=$postobj->MsgType;
            $type =$postObj->MsgType;
   $customevent=$postObj->Event;
            $latitude=$postObj->Location_X;
            $longitude=$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>";  
      $musicTpl="<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
          <CreateTime>%s</CreateTime>
           <MsgType><![CDATA[%s]]></MsgType>
           <Music>
                <Title><![CDATA[%s]]></Title>
              <Description><![CDATA[%s]]></Description>
                <MusicUrl><![CDATA[%s]]></MusicUrl>
                <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
                   </Music>
        </xml>";
             if($type=="event" )//and $customrevent=="subscribe")
            {
                 $msgType = "text";
                    $contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃"; 
                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                 echo $resultStr;
            }
            else 
   {
                switch($type)
                {
                    case "text":
                 {
                      $msgType = "text";
                     if(!empty( $keyword ))
                     {
                         
                         
                         if($keyword=="1")
                         {
                             $contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星";
                         }
                         else if($keyword=="2")
                         {
                             $contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合";
                         }
                         else if($keyword=="3")
                         {
                             $contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦";
                         }
                         else
                             $contentStr = "hi,感谢您的关注!您的请求不正确,请重新申请。祝您每天生活愉快,牙齿天天晒------熳熳";     
                     }
                     else
                     {
                         $contentStr= "Input something...";
                     }
                      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                     
                     if($keyword=="音乐")
                     {
                        $Title="纯真年代";
                        $Description="离婚前规则主题曲";
                        $MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3";
                        $HQMusicUrl= $MusicUrl;
                        $msgType="music";
                    $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl);     
                       
                     }
                     
                        break;     
                 }
                   
                    case "voice":
                    {
                        $msgType = "text";
                        $contentStr="语音功能尚未完善,请输入文本信息";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    case "image":
                    {
                        $msgType = "text";
                        $contentStr="你的图片很漂亮";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }                    
                    case "location":
                    {
                        $msgType = "text";
                        $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    case "link":
                    {
                        $msgType = "text";
                        $contentStr="你的链接有病毒吧!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    
                    default :
                    {
                        $msgType = "text";
                        $contentStr="此功能尚未开发";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                }
                
   }
      
       echo $resultStr;
  }
      
        
        
        else 
        {
         exit;
        }
 }
        
 function replymusic()       //回复音乐
 {
    $musicTpl="<xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>12345678</CreateTime>
    <MsgType><![CDATA[music]]></MsgType>
    <Music>
    <Title><![CDATA[纯真年代]]></Title>
    <Description><![CDATA[离婚前规则主题曲]]></Description>
    <MusicUrl><![CDATA[http://1.zhangxuman.sinaapp.com/music/1.mp3]]></MusicUrl>
    <HQMusicUrl><![CDATA[http://1.zhangxuman.sinaapp.com/music/1.mp3]]></HQMusicUrl>
    <ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>
    </Music>
    </xml>";
     $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
 }
    
 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;
  }
 
 }
}
?>

比上一篇博文多了一个“音乐”的功能,当输入“音乐”,就会回复“纯真年代”

转载于:https://my.oschina.net/zhangxuman/blog/340747

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值