微信公众号如何自定义回复文本图片与图文

自定义回复文本消息。想要回复信息用户发来的消息,首先我们需要获取用户发来的消息,接收地址就是微信公众号基本配置里写的地址。如果要回复文本消息我们需要将回复消息类型为text。
public function replyText(){
        if(IS_GET){
        	$this->display();
        	exit;
        }
        $data['content'] = I('content');
        $replyid = M('mp_reply_text')->add($data);
        $mp = $this->mp;
        $arr['mp_id'] = $mp['id'];
        $arr['type'] = 'text';
        $arr['keyword'] = I('keyword');
        $arr['reply_id'] = $replyid;

        M('mp_rule')->add($arr);
        $this->ajaxreturn(array('msg'=>'添加文本成功','url'=>U('replytext',['type'=>'text'])));
}

自定义回复图片消息。在这里我们需要注意的是微信接口的访问次数是有限制的,回复图片时需要将回复消息类型改为image。

public function replyImage(){

    	if(IS_GET){
        	$this->display();
        	exit;
        }
    	$keyword = I('post.keyword');
    	$media_id = I('post.media_id');
    	$url = I('post.url');
    	if(empty($keyword) || empty($url)){
    		$this->ajaxreturn(array('status'=>0,'msg'=>'必须输入关键字和选择图片'));
    		exit;
    	}

    	//选择本地图片,需上传到公众平台
    	if(empty($media_id)){
    		$accessToken = getAccess_token();
    		include APP_PATH . 'LaneWeChat/lanewechat.php';


    		//上传永久图片

    		$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";


    		$file = realpath('.'.$url);
    		$data['media'] = Curl::addFile($file);
    		$ret = Curl::callWebServer($api,$data,'post',1,0);

    		//上传成功
    		if(isset($ret['media_id'])){
    			$media_id = $ret['media_id'];
    			$url = $ret['url'];
    		}else{
    			$ret['fail'] = '本地图片上传公众平台失败';
    			$this->ajaxreturn(array('status'=>1,'msg'=>$ret));
    			exit;
    		}
    	}

    	$data['media_id'] = $media_id;
    	$data['url'] = $url;
    	$reply_id = M('mp_reply_image')->add($data);

    	$mp = $this->mp;
    	$arr['mp_id'] = $mp['id'];
    	$arr['type'] = 'image';
    	$arr['keyword'] = $keyword;
    	$arr['reply_id'] = $reply_id;
    	$ret = M('mp_rule')->add($arr);
    	if($ret){
    		$this->ajaxreturn(array('status'=>1,'msg'=>'添加图片成功','url'=>U('replyimage',['type'=>'image'])));
    	}else{
    		$this->ajaxreturn(array('status'=>1,'msg'=>'添加失败'));
    	}
    }
自定义回复图文消息。回复图文消息与回复图片消息基本上是一样的,只不过是多增加的几个字段而已。而且还需要修改一下回复消息类型就好。
public function replyNews(){
    	if(IS_GET){
        	$this->display();
        	exit;
        }
        $keyword = I('post.keyword');
        $title = I('post.title');
        $media_id = I('post.media_id');
        $content = I('post.content');
    	$picurl = I('post.content_source_url');
    	$url = I('post.url');

    	if(empty($keyword) || empty($url)){
    		$this->ajaxreturn(array('status'=>0,'msg'=>'必须输入关键字和选择图片'));
    		exit;
    	}

    	if(empty($media_id)){
    		$accessToken = getAccess_token();
    		include APP_PATH . 'LaneWeChat/lanewechat.php';

    		//上传永久图片

    		$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";

    		$file = realpath('.'.$url);
    		$data['media'] = Curl::addFile($file);
    		$ret = Curl::callWebServer($api,$data,'post',1,0);

    		//上传成功
    		if(isset($ret['media_id'])){
    			$media_id = $ret['media_id'];
    			$url = $ret['url'];
    		}else{
    			$ret['fail'] = '本地图片上传公众平台失败';
    			$this->ajaxreturn(array('status'=>1,'msg'=>$ret));
    			exit;
    		}
    	}
        
        $data['media_id'] = $media_id;
        $data['title'] = $title;
        $data['picurl'] = $url;
        $data['description'] = $content;  
    	$data['url'] = $picurl;
    	$reply_id = M('mp_reply_news')->add($data);
        $mp=getCurrentMp();
    	$mp = $this->mp;
    	$arr['mp_id'] = $mp['id'];
    	$arr['type'] = 'news';
    	$arr['keyword'] = $keyword;
    	$arr['reply_id'] = $reply_id;
    	$ret = M('mp_rule')->add($arr);
    	if($ret){
    		$this->ajaxreturn(array('status'=>1,'msg'=>'添加图文成功','url'=>U('replynews',['type'=>'news'])));
    	}else{
    		$this->ajaxreturn(array('status'=>1,'msg'=>'添加失败'));
    	}



你可能感兴趣的:(微信公众号如何自定义回复文本图片与图文)