zoukankan      html  css  js  c++  java
  • 微信邀请卡的开发

      1 <?php
      2 /**
      3   * wechat php test
      4   */
      5 
      6 //define your token
      7 define("TOKEN", "weixin");
      8 $wechatObj = new wechatCallbackapiTest();
      9 //验证服务器和公众平台是否连接成功
     10 //在服务器和公众平台验证成功之后,把$wechatObj->valid()注释掉
     11 // $wechatObj->valid();
     12 echo $wechatObj->responseMsg();
     13 
     14 class wechatCallbackapiTest
     15 {
     16     public function valid()
     17     {
     18         $echoStr = $_GET["echostr"];
     19 
     20         //valid signature , option
     21         if($this->checkSignature()){
     22             echo $echoStr;
     23             exit;
     24         }
     25     }
     26     //输出公众平台返回给用户的信息
     27     public function responseMsg()
     28     {
     29         //get post data, May be due to the different environments
     30         //相当于$_POST
     31         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
     32 
     33           //extract post data
     34         if (!empty($postStr)){
     35                 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
     36                    the best way is to check the validity of xml by yourself */
     37                 //只解析XML数据的主体部分,防止xxe攻击
     38                 libxml_disable_entity_loader(true);
     39                 //解析XML数据
     40                   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
     41                 //获取手机用户的OPenID
     42                 $fromUsername = $postObj->FromUserName;
     43                 //开发者微信号
     44                 $toUsername = $postObj->ToUserName;
     45                 //发送文本信息的关键字
     46                 $keyword = trim($postObj->Content);
     47                 //发送消息的类型
     48                 $type = $postObj->MsgType;
     49 
     50                 $time = time();
     51                 //发送文本信息的字符串模板
     52                 $textTpl = "<xml>
     53                             <ToUserName><![CDATA[%s]]></ToUserName>
     54                             <FromUserName><![CDATA[%s]]></FromUserName>
     55                             <CreateTime>%s</CreateTime>
     56                             <MsgType><![CDATA[%s]]></MsgType>
     57                             <Content><![CDATA[%s]]></Content>
     58                             <FuncFlag>0</FuncFlag>
     59                             </xml>";
     60                 //音乐消息的模板
     61                 $musicTpl = "<xml>
     62                             <ToUserName><![CDATA[%s]]></ToUserName>
     63                             <FromUserName><![CDATA[%s]]></FromUserName>
     64                             <CreateTime>%s</CreateTime>
     65                             <MsgType><![CDATA[%s]]></MsgType>
     66                             <Music>
     67                             <Title><![CDATA[%s]]></Title>
     68                             <Description><![CDATA[%s]]></Description>
     69                             <MusicUrl><![CDATA[%s]]></MusicUrl>
     70                             <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
     71                             
     72                             </Music>
     73                             </xml>";
     74                 //发送图文消息
     75                 $newsTpl="<xml>
     76                             <ToUserName><![CDATA[%s]]></ToUserName>
     77                             <FromUserName><![CDATA[%s]]></FromUserName>
     78                             <CreateTime>%s</CreateTime>
     79                             <MsgType><![CDATA[%s]]></MsgType>
     80                             <ArticleCount>%s</ArticleCount>
     81                             <Articles>
     82                             %s
     83                             </Articles>
     84                             </xml> ";                           
     85                 if($type == 'text'){
     86                     if(!empty( $keyword ))
     87                     {
     88                         // if($keyword == '音乐'){
     89                         //     $msgType = "music";
     90                         //     $title   = "See You Again";
     91                         //     $description='速度与激情7 原声大碟';
     92                         //     $music_url='http://www.yyzljg.com/wechat/music.mp3';
     93                         //     $high_url='http://www.yyzljg.com/wechat/music.mp3';
     94                         //     //格式化字符串
     95                         //     $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$description,$music_url,$high_url);
     96                         //     echo $resultStr;
     97                         // }
     98                         if($keyword=='音乐'){
     99                                 $msgType='music';
    100                                 $title='冰雪奇缘主题曲';
    101                                 $description='原声大碟';
    102                                 $music_url='http://www.yyzljg.com/wechat/music.mp3';
    103                                 $high_url='http://www.yyzljg.com/wechat/music.mp3';
    104                                 //格式化字符串
    105                                 $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$description,$music_url,$high_url);
    106                                 echo $resultStr;
    107                         }
    108                         if($keyword == '单图文'){
    109                             $msgType='news';
    110                             $counts=1;
    111                             $contentStr="<item>
    112                             <Title><![CDATA[大家一起学习微信开发]]></Title>
    113                             <Description><![CDATA[愿每天的你都是开心的!]]></Description>
    114                             <PicUrl><![CDATA[http://ibeliveone.applinzi.com/1.jpg]]></PicUrl>
    115                             <Url><![CDATA[http://ibeliveone.applinzi.com/1.jpg]]></Url>
    116                             </item>";
    117 
    118                             //格式化字符串
    119                             $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
    120                             echo $resultStr;
    121                         }
    122                         if($keyword == '多图文'){
    123                             $msgType='news';
    124                             $counts=4;
    125                             $contentStr='';
    126                             for($i=1;$i<=4;$i++){
    127                                 $contentStr.="<item>
    128                                 <Title><![CDATA[大家一起学习微信开发]]></Title>
    129                                 <Description><![CDATA[愿每天的你都是开心的!]]></Description>
    130                                 <PicUrl><![CDATA[http://ibeliveone.applinzi.com/{$i}.jpg]]></PicUrl>
    131                                 <Url><![CDATA[http://ibeliveone.applinzi.com/{$i}.jpg]]></Url>
    132                                 </item>";
    133                             }
    134                             //格式化字符串
    135                             $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
    136                             echo $resultStr;
    137                         } 
    138                         
    139                         //定义发送消息的类型
    140                         $msgType = "text";
    141                         //公众平台发送给用户的信息
    142                         $contentStr = "欢迎关注我的微信公众平台!";
    143                         //格式化字符串 
    144                         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    145                         echo $resultStr;die;               
    146                     
    147                     
    148                     }else{
    149                         echo "Input something...";
    150                     }
    151                 }elseif($type == 'image'){
    152                     //定义发送消息的类型
    153                     $msgType = "text";
    154                     //公众平台发送给用户的信息
    155                     $contentStr = "客官,您发送的美女好漂亮啊!";
    156                     //格式化字符串 
    157                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    158                     echo $resultStr; 
    159                     die;  
    160                 }elseif($type == 'voice'){
    161                     //定义发送消息的类型
    162                     $msgType = "text";
    163                     //公众平台发送给用户的信息
    164                     $contentStr = "客官,您的声音好动听啊!";
    165                     //格式化字符串 
    166                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    167                     echo $resultStr;
    168                     die;
    169                 }elseif($type == 'video'){
    170                     //定义发送消息的类型
    171                     $msgType = "text";
    172                     //公众平台发送给用户的信息
    173                     $contentStr = "客官,您分享的片片不是高清滴呦!";
    174                     //格式化字符串 
    175                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    176                     echo $resultStr;
    177                     die;
    178                 }else{
    179                     //定义发送消息的类型
    180                     $msgType = "text";
    181                     //公众平台发送给用户的信息
    182                     $contentStr = "客官,我书读的少,不知道你想要什么服务,回复关键字有惊喜:'音乐','单图文','多图文'";
    183                     //格式化字符串 
    184                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    185                     echo $resultStr;
    186                     die;
    187                 }                      
    188                 
    189 
    190         }else {
    191             echo "客官,我书读的少,不知道你想要什么服务,回复关键字有惊喜:'音乐','单图文','多图文'";
    192             exit;
    193         }
    194     }
    195         
    196     private function checkSignature()
    197     {
    198         // you must define TOKEN by yourself
    199         if (!defined("TOKEN")) {
    200             throw new Exception('TOKEN is not defined!');
    201         }
    202         
    203         $signature = $_GET["signature"];
    204         $timestamp = $_GET["timestamp"];
    205         $nonce = $_GET["nonce"];
    206                 
    207         $token = TOKEN;
    208         $tmpArr = array($token, $timestamp, $nonce);
    209         // use SORT_STRING rule
    210         sort($tmpArr, SORT_STRING);
    211         $tmpStr = implode( $tmpArr );
    212         $tmpStr = sha1( $tmpStr );
    213         
    214         if( $tmpStr == $signature ){
    215             return true;
    216         }else{
    217             return false;
    218         }
    219     }
    220 }
    221 
    222 ?>
  • 相关阅读:
    C#多线程中lock的用法
    杭州交通违规处理地点大全
    J2me c/s结构,访问数据库
    谁将成为下一个Twitter?最新最酷的创业奇思妙想
    那些相见恨晚的 JavaScript 技巧
    Adobe Dreamweaver CS5试用点滴感受
    python百炼成钢实例008输出 9*9 乘法口诀表
    python百炼成钢实例006斐波那契数列输出最后一个和整个数列,迭代
    python百炼成钢实例007复制,浅拷贝和深拷贝
    python百炼成钢实例080猴子分桃
  • 原文地址:https://www.cnblogs.com/CHEUNGKAMING/p/5809126.html
Copyright © 2011-2022 走看看