回复图片消息 (关注时自动回复图片)



valid(); //如果发来了echostr则进行验证
         }else{
             $wx->responseMsg(); //如果没有echostr,则返回消息
         }*/


        $timestamp = $_GET['timestamp'];//timestamp其实就是一个时间戳
        $nonce = $_GET['nonce'];//nonce是一个随机参数
        $token = "ruthweixin";//这个token填写你在微信公众平台上写的那个值
        $signature = $_GET['signature'];//这个signature其实就是在微信公众平台已经加密好的字符串
        $echostr = $_GET['echostr'];
        $array = array($timestamp, $nonce, $token);
        sort($array);

        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);

        if ($tmpstr == $signature && $echostr) {

            echo $echostr;
            exit;
        } else {
            $this->reponseMsg();
        }

    }


    public function reponseMsg()
    {
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string($postArr);
        if (strtolower($postObj->MsgType) == 'event') {
            //如果是关注事件(subscribe)
            if (strtolower($postObj->Event == 'subscribe')) {
                //回复用户消息
                /*$toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time = time();
                $msgType = 'text';
                $content = '欢迎关注 xxx,目前处测试开发状态,请期待!' . $postObj->FromUserName . '-' . $postObj->ToUserName;
                $template = "
							
							
							%s
							
							
							";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;*/

                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time = time();

                //获取微信用户信息,并生成专属海报
                $user_info = $this->getUserInfo($toUser);
                $nickname = $user_info['nickname'];
                $erwei_qr = $this->create($toUser,$nickname);
                $share_hb = $this->createHb($toUser,$nickname,$erwei_qr);
                $content = $this->uploadImage($share_hb);

              
                $msgType = 'image';
                $template = "
                            
							
                            %s
                            
                            
                            
                            
                            0
                            ";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;
            }
        }
//回复纯文本或单图文消息
        if (($postObj->MsgType) == 'text' && trim($postObj->Content) == '夏目友人帐') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $arr = array(
                array(
                    'title' => '夏目友人帐',
                    'description' => "此生无悔入夏目",
                    'picUrl' => 'http://img4.duitang.com/uploads/item/201508/16/20150816015528_X8dKY.jpeg',
                    'url' => 'http://www.shulvchen.cn',
                ),
            );
            $template = "
						
						
						%s
						
						" . count($arr) . "
						";
            foreach ($arr as $k => $v) {
                $template .= "
							<![CDATA[" . $v['title'] . "]]>
							
							
							
							";
            }
            $template .= "
						 ";
            echo sprintf($template, $toUser, $fromUser, time(), 'news');
        } else {
            switch (trim($postObj->Content)) {
                case 'bb':
                    $content = '我喜欢你';
                    break;
                case '良玉':
                    $content = '我喜欢你';
                    break;
                case '书旅':
                    $content = '加油';
                    break;
                case 'dsdf':
                    $content = '不愿错过他';
                    break;
                case '垒哥':
                    $content = '垒哥已死,有事儿烧纸';
                    break;
                case '书旅and良玉':
                    $content = 'Forever with you';
                    break;
                case '学弟':
                    $content = '书旅是你学长';
                    break;
                default:
                    $content = "百度一下,你就知道(点击文字,进入百度)";

            }
            $template1 = "
							
							
							%s
							
							
							";
            $fromUser = $postObj->ToUserName;//消息从哪里来
            $toUser = $postObj->FromUserName;//发送给谁
            $time = time();
            //$content  = '我喜欢你';
            $msgType = 'text';
            echo sprintf($template1, $toUser, $fromUser, $time, $msgType, $content);
        }
    }



    //上传临时素材,即将生成的海报放置微信服务器端,关注时返回图片media回复
    public function uploadImage($filepath) //$filepath
    {

        $type = 'image';
        //$filepath = "/data/web/web/Public/fenghui/share_own_hb/back_pic.jpg";
        $filedata = array("media" => "@".$filepath);
        $access_token  = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=$type";
        $result = call($url,$filedata);
        $arr_result = json_decode($result,true);
        //var_dump($arr_result);
        $meida_id = $arr_result['media_id'];
        return $meida_id;
    }




你可能感兴趣的:(微信)