报错 41005 media data missing 小程序

2019-02-22  本文已影响0人  谢谢子城

把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。

请求地址

array (
    'errcode' => 41005,
    'errmsg' => 'media data missing hint: [5D9ifa01908729]',
)


   /**
    *  上传图片到微信
    *
    * @throws \EasyWeChat\Kernel\Exceptions\HttpException
    * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
    * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
    * @throws \Psr\SimpleCache\InvalidArgumentException
    */
   public
   function uploadImg()
   {

       $accessToken = $this->getAccessTokens();
       $path = app()->basePath('public/id.jpg'); // 服务器图片路径

       $image = exif_imagetype($path);
       $type = image_type_to_mime_type($image); // 获取文件类型
       $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $accessToken . '&type=' . $type;

       // 启用 curl
       $curl = curl_init();

       if (class_exists('\CURLFile')) {
           curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
           $data = array('media' => new \CURLFile(realpath($path)));//>=5.5
       } else {
           if (defined('CURLOPT_SAFE_UPLOAD')) {
               curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
           }
           $data = array('media' => '@' . realpath($path));//<=5.5
       }

       // 请求 URl
       curl_setopt($curl, CURLOPT_URL, $url);
       // 是否 post 提交 0否 1是
       curl_setopt($curl, CURLOPT_POST, 1);
       // 提交的数据
       curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
       // 返回的内容作为变量储存,而不是直接输出。这个时候就必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true。
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
       // 设置请求头
       curl_setopt($curl, CURLOPT_USERAGENT, "TEST");
       // 检查是否有错误
       $error = curl_error($curl);

       if ($error) {
           // json 格式 转成数组
           return json_decode($error, true);
       } else {
           // json 格式 转成数组
           $result = curl_exec($curl); // 取出接口返回数据
           return json_decode($result, true);
       }
   }


{
    "type": "image",
    "media_id": "Liboq4RH01uvdyXEeK_bXnWgL-swePAfPeQbSGcNiJQ3H8JEFGPDNfLuuv2PZ3EC",
    "created_at": 1550762258
}
上一篇 下一篇

猜你喜欢

热点阅读