夺命雷公狗---微信开发10----玩转多图文消息回复

温馨提示以下,

如果返回的是多条图文消息Description参数则失效,返回一条则生效。

图文消息的图片链接支持png和jpg较好支持的

大图:300*200

小图:200*200

index.php 的代码如下:

 

<?php
/**
  * wechat php test
  */

//define your token
require_once "common.php";
define("TOKEN", "twgdh");
$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)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                // 使用simplexml技术对xml进行解析 
                // libxml_disable_entity_loader(true), 是从安全性考虑,为了防止xml外部注入,
                //只对xml内部实体内容进行解析
                libxml_disable_entity_loader(true);
                //加载 postStr 字符串
                  $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                file_put_contents(‘abc.log‘, "\r\n\r\n". $postStr, FILE_APPEND);
                $fromUsername = $postObj->FromUserName;
                file_put_contents(‘abc.log‘, "\r\n\r\n". $fromUsername, FILE_APPEND);
                $toUsername = $postObj->ToUserName;
                file_put_contents(‘abc.log‘, "\r\n\r\n". $toUsername, FILE_APPEND);
                $keyword = trim($postObj->Content);
                $time = time();
                global $tmp_arr;
                //根据接收到的消息类型,来进行分支处理(switch)
                switch($postObj->MsgType)
                {
                    case ‘event‘:
                        if($postObj->Event == ‘subscribe‘)
                        {
                            
                            $contentStr = "欢迎关注leigood微信测试号噢";
                            $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                            echo $resultStr; 
                        }
                        break;
                    case ‘text‘: //回复文本模块
                        //必须是以“图片”开头,后面并且是以数字结尾
                        if(preg_match("/^图片([0-9][0-9]*)$/u",$keyword,$matches)){
                            $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘);
                            mysql_select_db(‘wxdb‘,$connect);
                            mysql_query(‘set names utf8‘);
                            $matches = array();
                            preg_match("/^图片([0-9][0-9]*)$/u",$keyword,$matches); 
                            $sql = "select media_id from keep_image_uploads where id=$matches[1]";
                            $res = mysql_query($sql,$connect);
                            if($row = mysql_fetch_assoc($res)){
                                //先取出么media_id
                                $media_id = $row[‘media_id‘];
                                $resultStr = sprintf($tmp_arr[‘image‘], $fromUsername, $toUsername, $time, $media_id);
                                echo $resultStr;
                            }else{
                                $contentStr = ‘该图片还没上传噢!‘;
                                $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                                echo $resultStr;
                            }
                        }else if($keyword == ‘十八摸‘){
                            $title = ‘十八摸‘; //标题
                            $Description = ‘十八摸,您懂得~~~~~‘; //音乐描述
                            $MusicUrl = "http://weixin.showtp.com/music/gq1.mp3"; //音乐链接
                            $HQMusicUrl = "http://weixin.showtp.com/music/gq1.mp3"; //高质量音乐链接
                            
                            $resultStr = sprintf($tmp_arr[‘music‘], $fromUsername, $toUsername, $time, $title,$Description,$MusicUrl,$HQMusicUrl);
                            echo $resultStr;
                        }else if($keyword == ‘初夜泪流‘){
                            $title = ‘初夜泪流‘; //标题
                            $Description = ‘初夜泪流,时间宝贵的夜‘; //音乐描述
                            $MusicUrl = "http://weixin.showtp.com/music/gq2.mp3"; //音乐链接
                            $HQMusicUrl = "http://weixin.showtp.com/music/gq2.mp3"; //高质量音乐链接
                            
                            $resultStr = sprintf($tmp_arr[‘music‘], $fromUsername, $toUsername, $time, $title,$Description,$MusicUrl,$HQMusicUrl);
                            echo $resultStr;
                        }else if($keyword == ‘头条新闻‘){
                            $Title = ‘百度PK谷歌‘;
                            $Description = ‘在国际舞台上百度和谷歌的pk究竟相差多远呢?‘; //图文消息描述
                            $PicUrl = "http://weixin.showtp.com/image/logo.png"; 
                            //图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
                            $Url = "http://www.cyzone.cn/a/20150225/270130.html"; //点击图文消息跳转链接
                            $resultStr = sprintf($tmp_arr[‘singlenews‘], $fromUsername, $toUsername, $time, $Title, $Description, $PicUrl, $Url);
                            echo $resultStr;
                        }else if($keyword == ‘新闻‘){
                            //处理返回多条图文消息,但不能超过十条
                            $news_arr = array(
                                ‘news1‘=>array(‘title‘=>‘百度新闻‘,‘Description‘=>‘大陆百度很牛逼噢‘,
                                    ‘PicUrl‘=>‘http://weixin.showtp.com/image/logo.png‘,
                                    ‘Url‘=>‘http://www.baidu.com‘),
                                ‘news2‘=>array(‘title‘=>‘新浪新闻‘,‘Description‘=>‘大陆百度很牛逼噢‘,
                                    ‘PicUrl‘=>‘http://weixin.showtp.com/image/news1.png‘,
                                    ‘Url‘=>‘http://www.sina.com.cn‘),
                                ‘news3‘=>array(‘title‘=>‘腾讯新闻‘,‘Description‘=>‘大陆百度很牛逼噢‘,
                                    ‘PicUrl‘=>‘http://weixin.showtp.com/image/news2.png‘,
                                    ‘Url‘=>‘http://www.qq.com‘),
                                ‘news4‘=>array(‘title‘=>‘搜狐新闻‘,‘Description‘=>‘大陆百度很牛逼噢‘,
                                    ‘PicUrl‘=>‘http://weixin.showtp.com/image/news3.png‘,
                                    ‘Url‘=>‘http://www.sohu.com‘)
                                );
                                
                                $resultStr = sprintf($tmp_arr[‘newses‘], $fromUsername, $toUsername, $time, 
                                $news_arr[‘news1‘][‘title‘],
                                $news_arr[‘news1‘][‘Description‘],
                                $news_arr[‘news1‘][‘PicUrl‘],
                                $news_arr[‘news1‘][‘Url‘],
                                
                                $news_arr[‘news2‘][‘title‘],
                                $news_arr[‘news2‘][‘Description‘],
                                $news_arr[‘news2‘][‘PicUrl‘],
                                $news_arr[‘news2‘][‘Url‘],
                                
                                $news_arr[‘news3‘][‘title‘],
                                $news_arr[‘news3‘][‘Description‘],
                                $news_arr[‘news3‘][‘PicUrl‘],
                                $news_arr[‘news3‘][‘Url‘],
                                
                                $news_arr[‘news4‘][‘title‘],
                                $news_arr[‘news4‘][‘Description‘],
                                $news_arr[‘news4‘][‘PicUrl‘],
                                $news_arr[‘news4‘][‘Url‘]
                                
                                );
                            echo $resultStr;
                        
                        }else{
                            $contentStr = ‘您输入的格式有误‘;
                            $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                            echo $resultStr;
                        }
                        break;
                    case ‘image‘: //处理用户上传图片
                        $media_id = $postObj -> MediaId; //获取到用户上传的图片的mediaid
                        $resultStr = sprintf($tmp_arr[‘image‘], $fromUsername, $toUsername, $time, $media_id);
                        echo $resultStr;
                        
                        //将图片保存到本地服务器的文件系统
                        //1.先给图片创建一个名字
                        $image_file_name = time().‘.jpg‘;
                        //2.获取该图片的内容
                        $image_file = file_get_contents($postObj->PicUrl);
                        //3.保存到本地服务器的文件系统
                        //提醒:一定要保证您创建的文件夹是www用户可读可写,否则无法保存该图片到文件夹下
                        file_put_contents("./uploadimage/".$image_file_name,$image_file);
                        
                        //将图片的路径和相关信息入库
                        //1.创建一张表
                        //2.链接mysql数据库,并且添加图片信息
                        $connect = mysql_connect(‘localhost‘,‘root‘,‘root‘);
                        mysql_select_db(‘wxdb‘,$connect);
                        mysql_query(‘set names utf8‘);
                        $media_path = "./uploadimage/".$image_file_name; //路径
                        $sql = "insert into keep_image_uploads (id,openid,media_id,media_path) values(NULL,‘{$fromUsername}‘,‘{$media_id}‘,‘{$media_path}‘)";
                        mysql_query($sql,$connect);
                        break;
                    case ‘voice‘: //处理用户上传语言的业务逻辑
                        $media_id = $postObj -> MediaId; //获取media_id的id号
                        $resultStr = sprintf($tmp_arr[‘voice‘], $fromUsername, $toUsername, $time, $media_id);
                        echo $resultStr;
                        break;
                    case ‘location‘: //处理用户上传的地理位置信息
                        $Location_X = $postObj -> Location_X; //获取上传地理位置的纬度
                        $Location_Y = $postObj -> Location_Y; //获取上传地地理位置经度
                        $contentStr = "您上报的地理位置是:\n经度是:{$Location_Y} \n纬度是: {$Location_X}";
                        $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                        echo $resultStr;
                        break;
                        
                }
        }else {
            echo "";
            exit;
        }
    }
        
    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception(‘TOKEN is not defined!‘);
        }
        
        $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;
        }
    }
}

?>

 

 

 

common.php 代码如下:

<?php
    $tmp_arr = array(
    ‘text‘ => <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>
XML
,
    ‘image‘=> <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>
XML
,
    ‘voice‘=> <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>
XML
,
    ‘music‘=> <<<XML
<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>
XML
,
    ‘singlenews‘=> <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title> 
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml> 
XML
,
    ‘newses‘=> <<<XML
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>4</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>
<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> 
XML
    );

 

 

用到的图片如下:

 

夺命雷公狗---微信开发10----玩转多图文消息回复

 

 

夺命雷公狗---微信开发10----玩转多图文消息回复

 

 

夺命雷公狗---微信开发10----玩转多图文消息回复

 

 

夺命雷公狗---微信开发10----玩转多图文消息回复

夺命雷公狗---微信开发10----玩转多图文消息回复

上一篇:微信SDK导入报错 Undefined symbols for architecture i386:"operator delete[](void*)", referenced from:


下一篇:python 脚本查看微信把你删除的好友--win系统版