首页  >  文章  >  后端开发  >  php7中的curl文件上传出现错误该怎么办

php7中的curl文件上传出现错误该怎么办

醉折花枝作酒筹
醉折花枝作酒筹转载
2021-08-13 09:20:282498浏览

本篇文章给大家介绍一下解php7中curl文件上传出现错误的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

php7中的curl文件上传出现错误该怎么办

最近在项目跟微信公众号的素材库对接接口,采用curl的post方式提交素材文件,发现一直提示

{“errcode”:41005,”errmsg”:”media data missing”}

代码内容

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => '@' . $fileName,
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
$data = is_array($data) ? http_build_query($data) : $data;
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);

查阅了官方文档 在php5.5后不再支持@,必须要使用CurlFile或者设置CURLOPT_SAFE_UPLOAD为1

There are “@” issue on multipart POST requests.
Solution for PHP 5.5 or later:
Enable CURLOPT_SAFE_UPLOAD.
Use CURLFile instead of “@”.

在php7 curl如果改变CURLOPT_SAFE_UPLOAD会提示一个错误 如下: 

curl_setopt(): Disabling safe uploads is no longer supported in 报错

我们只能老老实实使用CurlFile来处理

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
$data = is_array($data) ? http_build_query($data) : $data;
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);

然后发现这样写三个大坑(是我自己蠢)

1、如果CURLOPT_POSTFILEDS传入的是数组 content_type就为multipart/form-data;如果CURLOPT_POSTFILEDS传入的是json或者key-value& content_type就为x-www-form_urlencoded;但是微信支持form-data传递的数组 

2、数组里面如果有包含对象对其进行http_build_query会将其改成数组 

3、CurlFile只能读取服务器内的路径,如果要上传网上的地址,需要先下载到服务器的临时目录,在通过CurlFile读取文件路径(绝对路径)

所以我们接着调整代码

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);

正当我以为我可以解脱的时候,php7这里弹出一个notice语法错误: 

Array to string conversion 

然后查阅了资料 发现CURLOPT_POSTFIEDLDS不支持多维数组 

但是提示的notice的语法错误,我们完全可以进行屏蔽 

继续调整代码

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
curl_setopt(self::$curl, CURLOPT_POST, 1);
@curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);

结果终于上传素材成功了 

抬头一望 天已黑 

开心我赶紧一边擦鼻涕一边收拾东西下班

推荐学习:php视频教程

以上是php7中的curl文件上传出现错误该怎么办的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:csdn.net。如有侵权,请联系admin@php.cn删除