• PHP微信开发代码


    1,SAE上申请服务器

    2,绑定测试账号

    3,Token验证

    <?php
    /*
        http://www.cnblogs.com/xrhou12326/
        CopyRight 2014 All Rights Reserved
    */
    define("TOKEN", "xrhou");
    
    $wechatObj = new wechatCallbackapiTest();
    if (!isset($_GET['echostr'])) {
        $wechatObj->responseMsg();
    }else{
        $wechatObj->valid();
    }
    
    class wechatCallbackapiTest
    {
        //验证消息
        public function valid()
        {
            $echoStr = $_GET["echostr"];
            if($this->checkSignature()){
                echo $echoStr;
                exit(0);
            }
        }
    
        //检查签名
        private function checkSignature()
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
    
            if($tmpStr == $signature){
                return true;
            }else{
                return false;
            }
        }
    
        //响应消息
        public function responseMsg()
        {
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            if (!empty($postStr)){
                $this->logger("R ".$postStr);
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $RX_TYPE = trim($postObj->MsgType);
    
                switch ($RX_TYPE)
                {
                    case "event":
                        $result = $this->receiveEvent($postObj);
                        break;
                    case "text":
                        $result = $this->receiveText($postObj);
                        break;
                    case "image":
                        $result = $this->receiveImage($postObj);
                        break;
                    case "location":
                        $result = $this->receiveLocation($postObj);
                        break;
                    case "voice":
                        $result = $this->receiveVoice($postObj);
                        break;
                    case "video":
                        $result = $this->receiveVideo($postObj);
                        break;
                    case "link":
                        $result = $this->receiveLink($postObj);
                        break;
                    default:
                        $result = "unknown msg type: ".$RX_TYPE;
                        break;
                }
                $this->logger("T".$result);
                echo $result;
            }else {
                echo "";
                exit(0);
            }
        }
    
        //接收事件消息
        private function receiveEvent($object)
        {
            $content = "";
            switch ($object->Event)
            {
                case "subscribe":
                    $content = "欢迎关注 yabeBlog 微信订阅号,你的关注是我们进步的动力!回复 1 了解详情;回复 2 查看最新动态;回复 3 查看最美blog; ";
                    $content .= (!empty($object->EventKey))?("
    来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
                    break;
                case "unsubscribe":
                    $content = "取消关注";
                    break;
                case "SCAN":
                    $content = "扫描场景 ".$object->EventKey;
                    break;
                case "CLICK":
                    switch ($object->EventKey)
                    {
                        case "COMPANY":
                            $content = "yabeBlog,技术改变世界,代码成就未来!";
                            break;
                        default:
                            $content = "点击菜单:".$object->EventKey;
                            break;
                    }
                    break;
                case "LOCATION":
                    $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
                    break;
                case "VIEW":
                    $content = "跳转链接 ".$object->EventKey;
                    break;
                default:
                    $content = "receive a new event: ".$object->Event;
                    break;
            }
            $result = $this->transmitText($object, $content);
            return $result;
        }
    
        //接收文本消息
        private function receiveText($object)
        {
            switch ($object->Content)
            {
                case "1":
                    $content = array();
               		$content[] = array("Title"=>"yabeBlog,技术改变世界,代码成就未来!","Description"=>"yabeBlog,Your write anything word!","PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg","Url"=>"http://mp.weixin.qq.com/s?__biz=MzA3NjM3MjQxOA==&mid=200218642&idx=1&sn=1a901b68dd5941035c20027635be5e29#rd");
                    break;
                case "图文":
                case "单图文":
                    $content = array();
                    $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=xrhou12326");
                    break;
                case "2":
                    $content = array();
                    $content[] = array("Title"=>"yabeBlog,技术改变世界,代码成就未来!", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=xrhou12326");
                    $content[] = array("Title"=>"舌尖上的中国2 时间的味道", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=xrhou12326");
                    $content[] = array("Title"=>"yabeBlog,下一个目标", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=xrhou12326");
                    break;
                case "音乐":
                    $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://music.baidu.com/album/63594", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
                    break;
                default:
                    $content = date("Y-m-d H:i:s",time());
                    break;
            }
            if(is_array($content)){
                if (isset($content[0]['PicUrl'])){
                    $result = $this->transmitNews($object, $content);
                }else if (isset($content['MusicUrl'])){
                    $result = $this->transmitMusic($object, $content);
                }
            }else{
                $result = $this->transmitText($object, $content);
            }
            return $result;
        }
    
        //接收图片消息
        private function receiveImage($object)
        {
            $content = array("MediaId"=>$object->MediaId);
            $result = $this->transmitImage($object, $content);
            return $result;
        }
    
        //接收位置消息
        private function receiveLocation($object)
        {
            $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
            $result = $this->transmitText($object, $content);
            return $result;
        }
        //接收语音消息
        private function receiveVoice($object)
        {
            if (isset($object->Recognition) && !empty($object->Recognition)){
                $content = "你刚才说的是:".$object->Recognition;
                $result = $this->transmitText($object, $content);
            }else{
                $content = array("MediaId"=>$object->MediaId);
                $result = $this->transmitVoice($object, $content);
            }
            return $result;
        }
        //接收视频消息
        private function receiveVideo($object)
        {
            $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
            $result = $this->transmitVideo($object, $content);
            return $result;
        }
        //接收链接消息
        private function receiveLink($object)
        {
            $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
            $result = $this->transmitText($object, $content);
            return $result;
        }
        //回复文本消息
        private function transmitText($object, $content)
        {
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    </xml>";
            $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
            return $result;
        }
    
        //回复图片消息
        private function transmitImage($object, $imageArray)
        {
            $itemTpl = "<Image>
        <MediaId><![CDATA[%s]]></MediaId>
    </Image>";
    
            $item_str = sprintf($itemTpl, $imageArray['MediaId']);
    
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[image]]></MsgType>
    $item_str
    </xml>";
    
            $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
            return $result;
        }
    
        //回复语音消息
        private function transmitVoice($object, $voiceArray)
        {
            $itemTpl = "<Voice>
        <MediaId><![CDATA[%s]]></MediaId>
    </Voice>";
    
            $item_str = sprintf($itemTpl, $voiceArray['MediaId']);
    
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[voice]]></MsgType>
    $item_str
    </xml>";
    
            $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
            return $result;
        }
    
        //回复视频消息
        private function transmitVideo($object, $videoArray)
        {
            $itemTpl = "<Video>
        <MediaId><![CDATA[%s]]></MediaId>
        <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
    </Video>";
    
            $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);
    
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[video]]></MsgType>
    $item_str
    </xml>";
    
            $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
            return $result;
        }
    
        //回复图文消息
        private function transmitNews($object, $newsArray)
        {
            if(!is_array($newsArray)){
                return;
            }
            $itemTpl = "    <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
        </item>";
            $item_str = "";
            foreach ($newsArray as $item){
                $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
            }
            $newsTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <Content><![CDATA[]]></Content>
    <ArticleCount>%s</ArticleCount>
    <Articles>
    $item_str</Articles>
    </xml>";
    
            $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
            return $result;
        }
    
        //回复音乐消息
        private function transmitMusic($object, $musicArray)
        {
            $itemTpl = "<Music>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <MusicUrl><![CDATA[%s]]></MusicUrl>
        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
    </Music>";
    
            $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
    
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[music]]></MsgType>
    $item_str
    </xml>";
            $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
            return $result;
        }
    
        //日志记录
        private function logger($log_content)
        {
            if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
                sae_set_display_errors(false);
                sae_debug($log_content);
                sae_set_display_errors(true);
            }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
                $max_size = 10000;
                $log_filename = "log.xml";
                if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
                file_put_contents($log_filename, date('H:i:s')." ".$log_content."
    ", FILE_APPEND);
            }
        }
    }
    ?>
    

    5,菜单设置

    <?php
    
    $access_token = "2ADRGuCq8UupgDLD1_CbP4I0klAmELoWBL_CQLcxG8q_Nb5hIoC-2WERSjs4VxH_Ht7gD35MRcxEs6U33hLuJA";
    
    $jsonmenu = '{
        "button": [
            {
                "name": "公司频道", 
                "sub_button": [
                    {
                        "type": "view", 
                        "name": "公司介绍", 
                        "url": "http://www.yabeblog.me/"
                    },
    		{
                        "type": "view", 
                        "name": "公司动态", 
                        "url": "http://www.yabeblog.me/"
                    },
    		{
                        "type": "view", 
                        "name": "产品管理", 
                        "url": "http://www.yabeblog.me/"
                    },
    		{
                        "type": "view", 
                        "name": "知识管理", 
                        "url": "http://www.yabeblog.me/"
                    }, 
                    {
                        "type": "view", 
                        "name": "成功案例", 
                        "url": "http://www.yabeblog.me/"
                    }
                ]
            }, 
            {
                "name": "员工频道", 
                "sub_button": [
                    {
                        "type": "view", 
                        "name": "技术支持", 
                        "url": "http://www.yabeblog.me/"
                    }, 
                    {
                        "type": "view", 
                        "name": "联系我们", 
                        "url": "http://www.yabeblog.me/"
                    }, 
                    {
                        "type": "click", 
                        "name": "赞一下我们", 
                        "key": "V1001_GOOD"
                    }
                ]
            }
        ]
    }';
    
    
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
    $result = https_request($url, $jsonmenu);
    var_dump($result);
    
    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    ?>
  • 相关阅读:
    2017-2018-2 20155206 《网络对抗技术》 实验六:信息搜集与漏洞扫描
    20155206 Exp5 MSF基础应用
    20155206 实验4 恶意代码分析
    20155206《网络攻防》第三次实验_免杀及其原理
    20155206 Exp2 后门原理与实践
    20155206赵飞 Exp1PC平台逆向破解及Bof基础实践
    J-19 集合对象
    J-18 集合对象
    J-17 集合对象
    J-16 集合对象
  • 原文地址:https://www.cnblogs.com/xrhou12326/p/5159570.html
Copyright © 2020-2023  润新知