微信公众号上传素材提示41005的问题处理
{"errcode":41005,"errmsg":"media data missing hint: [9EgIfa06588236] rid: 604f738a-74205889-74579fe8"}
利用curl上传素材发现高版本的php出现上面的错误提示,无法成功上传素材。 查阅相关资料得知是php版本的影响,兼容处理如下:
/** * 上传临时素材:图片,语音,视频,缩略图 * 储存到微信公众平台服务器,3天 * 可通过上传后返回的media_id再次去取得该图片 * @param $file * @param $type image|voice|video|thumb * @return mixed */ public function uploadTmp($file, $type) { $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $this->get_access_token() . '&type=' . $type; if (class_exists('\CURLFile')) { $data['media'] = new \CURLFile(realpath($file)); } else { $data['media'] = '@'.realpath($file); } $result = $this->curl_post($url, $data); return json_decode($result); } /** * post网络请求 * @param $url * @param string $data * @param int $timeout * @return bool|string */ public function curl_post($url, $data = '', $timeout = 10) { if ($url == "" || $timeout <= 0) { return false; } $con = curl_init($url); curl_setopt($con, CURLOPT_HEADER, false); curl_setopt($con, CURLOPT_RETURNTRANSFER, true); curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout); curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);//调试去掉ssl验证 // POST数据 curl_setopt($con, CURLOPT_POST, true); // 把post的变量加上 curl_setopt($con, CURLOPT_POSTFIELDS, $data); return curl_exec($con); }
上面核心的内容:
if (class_exists('\CURLFile')) { $data['media'] = new \CURLFile(realpath($file)); } else { $data['media'] = '@'.realpath($file); }
调用:
$this->uploadTmp(DIR_LOG.'data/img/ac.png','image');
微信正常返回:
{"type":"image","media_id":"WZ_x7-RS5O3PHIWW6P5DAb_RaPkAWtASna1Fl41ehg2WfgQGCY-gLtd_KOnsk-N5","created_at":1615820789,"item":[]}