微信php7.1上传新增图片素材出现"errcode":41005,"errmsg":media data missing hint问题

原因很大部分是php版本问题
此次测试版本php7.1

问题原因是:
//php7不支持array('media' => '@'.$path_img) 我的问题就出现在这,因为微信官方示例是'@'.$path_img所以出错了。

html代码:
php 代码: //上传方法 public function upload() { $tmp_name = $_FILES['file']['tmp_name']; $path_img = 'upload/'.$_FILES['file']['name'];//设置图片路径 move_uploaded_file($tmp_name,$path_img); //将form 上传过来到图片移到指定目录 $authorizer_access_token="授权方的authorizer_access_token"; $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$authorizer_access_token&type=image"; $curl = curl_init (); //这一步想明白参考:https://segmentfault.com/a/1190000000725185 if (class_exists ( '\CURLFile' )) { $filedata=array('media' => new \CURLFile(realpath($path_img))); curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true ); } else { $filedata=array('media' => '@'.$path_img); if (defined ( 'CURLOPT_SAFE_UPLOAD' )) { curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false ); } } curl_setopt ( $curl, CURLOPT_URL, $url ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); if (! empty ( $filedata )) { curl_setopt ( $curl, CURLOPT_POST, 1 ); curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata ); } curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); $output = curl_exec ( $curl ); curl_close ( $curl ); return $output; }

你可能感兴趣的:(PHP)