温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php7中的curl文件上传出现错误怎么解决

发布时间:2021-08-13 10:09:25 来源:亿速云 阅读:280 作者:chen 栏目:编程语言

这篇文章主要讲解了“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);

结果终于上传素材成功了

感谢各位的阅读,以上就是“php7中的curl文件上传出现错误怎么解决”的内容了,经过本文的学习后,相信大家对php7中的curl文件上传出现错误怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI