关于微信开发的自动回复之图片消息

回复图片消息和文本消息都是需要用两个数据表将它管理起来


接下来将数据存入数据库,首先获取到前台提交的数据,然后将数据分别写入两个数据表中,并将上传的图片同时上传到公众号服务器上,这时又要用到微信上传素材的接口,具体代码如下:

//回复图片
	public function addImage(){
		$keyword=I('post.keyword');
		$media_id=I('post.media_id');
     	$url=I('post.url');
     	//相对路径->绝对路径
		$file=realpath('.'.$url);
		// echo $file;
		// exit;
     	
     	if (empty($keyword) || empty($url)) {
     		$this->ajaxReturn(array('msg'=>"必须填写关键字"));
     		exit;
     	}
     	if(empty($media_id)){
     		$accessToken=getAccess_token();
		    include APP_PATH . 'LaneWeChat/lanewechat.php';
		    //上传永久图片api
		    $api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
		    $data['media']=Curl::addFile($file);
		    $ret=Curl::callWebServer($api,$data,'post',true,false);
		    // print_r($ret);
		    // exit;

     	}
     	if (isset($ret['media_id'])) {
     		$mp=$this->mp;
		    $data['media_id']=$ret['media_id'];
            $data['url']=$ret['url'];
            $model=M('mp_reply_image');
     	    $ret=$model->add($data);
     	    if(isset($ret)){
     	    	$arr['mpid']=$mp['id'];
		        $arr['reply_id']=$ret;
		        $arr['type']='image';
		     	$arr['keyword']=$keyword;
		        $rule=M('mp_rule');
		     	$ret=$rule->add($arr);
     	    }
     	    if ($ret) {
     	    	$this->ajaxReturn(array('status'=>0,'msg'=>"添加成功!"));
     	    } else {
     	    	$this->ajaxReturn(array('msg'=>$ret));
     	    }
     	    
		}else{
          $ret['fail']='本地上传失败';
          $this->ajaxReturn(array('status'=>1,'msg'=>$ret));
          exit;
        }
     	
	}

猜你喜欢

转载自blog.csdn.net/ssh456/article/details/79993851