获取永久素材
http请求方式: POST,https协议
https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN
参数:
access_token | 是 | 调用接口凭证 |
media_id | 是 | 要获取的素材的media_id |
直接返回:"https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN" -d '{"media_id":"61224425"}' > file
错误返回:{"errcode":40007,"errmsg":"invalid media_id"}
创建新增图文的方法:replynews
public function replynews(){
if(IS_GET){
$this->display();
}else{
$keyword=I('keyword');
$title = I('title');
$description = I('content');
// echo $description;
// exit;
$url = I('content_source_url');
$fileurl = I('url');
// dump($picurl);
// exit;
// if(empty($keyword)|| empty($description)){
// $this->ajaxReturn(array('status'=>0,'msg'=>'请输入关键字和图片'));
// exit;
// }
$accessToken=getAccess_token();
// echo $accessToken;
// exit;
include APP_PATH.'LaneWeChat/lanewechat.php';
$api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=news";
$file=realpath('.'.$fileurl);
// echo $file;
// exit;
$data['media']=curl::addFile($file);
$ret=Curl::callwebServer($api,$data,'post',1,0);
// dump($ret);
// exit;
if(isset($ret['url'])){
// $media_id=$ret['media_id'];
$picurl=$ret['url'];
// echo $picurl;
// exit;
}else{
$ret['fail']='上传失败';
$this->ajaxReturn(array('status'=>1,'msg'=>$ret));
exit;
}
$data['title'] = $title;
$data['description'] = $description;
$data['url'] = $url;
$data['picurl'] = $picurl;
$reply_id=M('mp_reply_news')->add($data);
$mp=getCurrentMp();
$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'=>'replyimage'));
}else{
$this->ajaxReturn(array('status'=>0,'msg'=>'添加失败'));
}
}
}
回复图文的方法
public static function text(&$request){
// $content = '收到文本消息';
// return ResponsePassive::text($request['fromusername'], $request['tousername'], $content);
//获取那个公众号发过来的请求
$mpid=$_GET['id'];
$content=$request['content'];
$where['mp_id']=$mpid;
$where['keyword']=$content;
$data=M('mp_rule')->where($where)->find();
if($data){
//发送请求中有这个关键字
$reply_id=$data['reply_id'];
$type=$data['type'];//回复类型
//判断回复类型
switch($type){
case 'text'://回复文字
$reply=M('mp_reply_text')->find($reply_id);
if($reply){
$reply_text=$reply['content'];
}else{
$reply_text='出错啦';
}
return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
break;
case 'image'://回复图片
$reply = M('mp_reply_image')->find($reply_id);
if($reply){
$media_id = $reply['media_id'];
return ResponsePassive::image($request['fromusername'],$request['tousername'],$media_id);
}else{
$reply_text = "出错啦";
return ResponsePassive::text($request['fromusername'], $request['tousername'], $reply_text);
}
break;
case 'news'://回复图文
$reply=M('mp_reply_news')->find($reply_id);
if($reply){
$item[]=ResponsePassive::newsItem($reply['title'],$reply['description'],$reply['picurl'],$reply['url']);
return ResponsePassive::news($request['fromusername'],$request['tousername'],$item);
}else{
$reply_text='出错啦';
return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
}
break;
default:
return 'success';
break;
}
}else{
return 'success';
}
}