$imgpath = UPLOADS_WE_PATH . $management_data['cover']; if (!$management_data['cover'] && !file_exists($imgpath)) { return jsons(['status' => 0, 'msg' => '素材不存在']); } $media = json_decode($this->grant->uploadImg($access_token, $imgpath, $order = 2), true); if ($media == '') { return jsons(['status' => 0, 'msg' => '保存失败']); } if (!array_key_exists('media_id', $media)) { return jsons(['status' => 0, 'msg' => '保存失败']); } $preg = '/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/'; preg_match_all($preg, $management_data['content'], $imgArr); foreach ($imgArr[1] as $key => $val) { $img_path = UPLOADS_WE_PATH . $val; $media_url = json_decode($this->grant->uploadImg($access_token, $img_path, $order = 2), true); if ($media_url == '') { return jsons(['status' => 0, 'msg' => '保存失败']); } $management_data['content'] = str_replace($val, $media_url['url'], $management_data['content']); } $data_material = [ 'articles' => [ [ 'title' => urlencode($management_data['title']), 'thumb_media_id' => $media['media_id'], 'author' => urlencode($management_data['author']), 'digest' => urlencode($management_data['digest']), 'content' => $management_data['content'], 'show_cover_pic' => '1', 'content_source_url' => urlencode($management_data['content_source_url']), ] ] ]; $material = json_decode($this->grant->material($access_token, $data_material), true); if (!array_key_exists('media_id', $material)) { return jsons(['status' => 0, 'msg' => '保存失败']); }
需要注意的是 先要上传封面获取上传封面的id,图文素材的图片url 要用img src 标签 因为微信会自动过滤
/** * 新增永久图文素材 * @param $title */ public function material($access_token, $data, $type = 1){ if ($type == 1) { $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$access_token"; } else if($type == 2) { $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=$access_token"; } else if($type == 3){ $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=$access_token"; } $resultData = $this->https_request($url, json_encode($data)); return $resultData; } //上传素材并获取唯一标识 public function uploadImg($access_token, $filepath,$type = '1') { if($type == '1'){ $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=image"; // 1代表上传临时图片素材 } else if($type == '2') { $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image"; // 2代表上传永久图片素材 }else if($type == '3'){ $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token"; // 3代表上传永久图文返回url } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POST, 1); $data = array('media' => new \CURLFile($filepath));//php5.6 //$data = array('media' => '@' . $filepath);//php5.5 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); if (curl_errno($curl)) { return 'Errno' . curl_error($curl); } curl_close($curl); return $result; }