关于微信公众号开发中的回复功能

    public function text(){
        $this->display('replytext');
    }

    //回复文本
    public function replyText(){
        $model=M('mp_reply_text');
        $content=I('post.content');
        // dump($content);
        $arr['content']=$content;
        $ret=$model->add($arr);
        // dump($ret);
        $model=M('mp_rule');
        $mp=getCurrentMp();
        $arr=array(
            'mpid'=>$mp['id'],
            'keyword'=>I('post.keyword'),
            'type'=>'text',
            'reply_id'=>$ret
            );
        $model->add($arr);
        $this->ajaxReturn(array('msg'=>'添加成功','url'=>U('index')));
    }

以上是关于关键字回复的代码,首先需要指向模板,实例化表然后将用户输入的内容存入数据库中。

//回复图片
	public function image(){
		$this->display('replyimage');
	}
	public function replyImage(){
		$keyword = I('keyword');
		$media_id = I('media_id');
		$url = I('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=getCurrentMp();
			$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')));
			}else{
				$this->ajaxReturn(array('status'=> 0 , 'msg' => '添加失败!'));
			}
		}
	}

以上是发送内容回复图片的功能,比较重要的值就是mediaid,因为如果没有这个值得话程序肯定会出问题的,然后和关键字回复的功能一样,将用户输入的数据存入指定的数据表中。

public function news(){
		$this->display('replynews');
	}
	public function replyNews(){
			$result=I('post.');
			if(empty($result['keyword'])|| empty($result['url'])){
				$this->ajaxReturn(array('status'=>0,'msg'=>'必须输入关键字和选择图片'));
				exit;
			}
			if(empty($result['media_id'])){
				$accessToken=getAccess_token();
				include APP_PATH . 'LaneWeChat/lanewechat.php';	
				$url1="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
				$file=realpath('.'. $result['url']);//相对路径->绝对路径
				$data['media']=Curl::addFile($file);			
				$ret=Curl::callWebServer($url1,$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['title']=$result['title'];
				$data['description']=$result['content'];
				$data['picurl']=$url;
				$data['url']=$result['content_source_url'];
				$reply_id=M('mp_reply_news')->add($data);
				$mp=getCurrentMp();
				$arr=array(
					'mpid'=>$mp['id'],
					'type'=>'news',
					'keyword'=>$result['keyword'],
					'reply_id'=>$reply_id
					);
				$ret=M('mp_rule')->add($arr);
				if($ret){
					$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功','url'=>U('replynews')));
				}else{
					$this->ajaxReturn(array('status'=>0,'msg'=>'添加失败'));
				}
			}
	}

以上就是关于图文回复的功能的代码,其实这三个功能来说,只有图文比较复杂,但是,图文其实可以根据图片的部分代码进行改编即可了。


总的来说,以前总觉得做微信这些功能很简单,只需要在腾讯服务器进行操作就行,其实实际上如果让我们自己通过第三方来进行实现的话还是比较费时间的,收获很大。

猜你喜欢

转载自blog.csdn.net/k363890296/article/details/79995316