微信開發--訊息回覆(文字,圖片,圖文)
阿新 • • 發佈:2019-01-22
public function replyText(){ $data['content']=I('content'); $replyid=M('mp_reply_text' )->add($data); if($replyid){ $mp = $this->mp; $arr['keyword']=I('keyword'); $arr['reply_id'] = $replyid; $arr['mp_id'] = $mp['id']; $arr['type'] = 'text'; $ret = M('mp_rule')->add($arr); if ($ret) { $this->ajaxReturn(array('msg'=>'新增成功!','url'=>U('index'))); }else{ } }else{ } } public function image(){ $this->display('replyimage'); } public function replyImage(){ $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'=>'必須輸入關鍵字和選擇圖片')); } if(empty($media_id)){ $access_token=getAccess_token(); include APP_PATH . 'LaneWeChat/lanwechat.php'; $api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image"; $file=realpath('.'.$url); $data['media']=Curl::addFile($file); // var_dump($data); // exit; $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('msg'=>'新增成功!','url'=>('image'))); } } public function news(){ $this->display('replynews'); } public function replynews(){ $data = array(); $url = I('post.url'); //圖片路徑 $file = realpath('.' . $url); //相對路徑轉絕對路徑 $access_token = getAccess_token(); include APP_PATH .'LaneWeChat/lanewechat.php'; $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image"; $data['media'] = '@'.$file; $ret = Curl::callWebServer($url,$data,'post',true,false); if (isset($ret['media_id'])) { $mp = $this->mp; $mp_id = $mp['id']; $data['picurl'] = $ret['url']; $data['title'] = I('post.title'); $data['description'] = I('post.content'); $data['url'] = I('content_source_url'); $newsret = M('mp_reply_news')->add($data); if ($newsret) { $data['keyword'] = I('post.keyword'); $data['reply_id'] = $newsret; $data['mp_id'] = $mp_id; $data['type'] = 'news'; if ($mp['is_use'] == 1){ $data['status'] = 1; }else{ $data['status'] = 0; } $ret = M('mp_rule')->add($data); if ($ret) { $this->ajaxReturn(array('msg'=>'新增成功!','url'=>('news'))); }else{ $this->ajaxReturn(array('msg'=>$ret)); } } }else{ $this->ajaxReturn(array('msg'=>$ret)); } }