Maison> développement back-end> PHP7> le corps du texte

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

醉折花枝作酒筹
Libérer: 2023-02-18 07:56:01
avant
2438 Les gens l'ont consulté

本篇文章给大家介绍一下解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);
Copier après la connexion

查阅了官方文档 在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);
Copier après la connexion

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

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);
Copier après la connexion

正当我以为我可以解脱的时候,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);
Copier après la connexion

结果终于上传素材成功了

抬头一望 天已黑

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

推荐学习:php视频教程

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:csdn.net
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!