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

本文介绍了一个基于ThinkPHP框架实现的微信公众号登录验证及消息自动响应功能。该功能包括了对接微信服务器的签名验证、关注事件处理、文本消息响应及特定关键字触发的图文消息回复等核心操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



<?php
namespace Home\Controller;

use Think\Controller;

class WxController extends Controller
{
    //检查是否登录
    public function _initialize()
    {
        header('content-type:text/html;charset=utf-8');
        define("TOKEN", "ruthweixin");

    }


    public function index()
    {
        /* include_once("./wechatCallbackapiTest.php");
         $wx = new \wechatCallbackapiTest();

         if($_GET['echostr']){
             $wx->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 = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
                $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 = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Image>
                            <MediaId><![CDATA[%s]]></MediaId>
                            </Image>
                            <FuncFlag>0</FuncFlag>
                            </xml>";
                $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 = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<ArticleCount>" . count($arr) . "</ArticleCount>
						<Articles>";
            foreach ($arr as $k => $v) {
                $template .= "<item>
							<Title><![CDATA[" . $v['title'] . "]]></Title>
							<Description><![CDATA[" . $v['description'] . "]]></Description>
							<PicUrl><![CDATA[" . $v['picUrl'] . "]]></PicUrl>
							<Url><![CDATA[" . $v['url'] . "]]></Url>
							</item>";
            }
            $template .= "</Articles>
						</xml> ";
            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 = "<a href='http://www.baidu.com'>百度一下,你就知道(点击文字,进入百度)</a>";

            }
            $template1 = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
            $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;
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值