Home Backend Development PHP7 What should I do if there is an error in curl file upload in php7?

What should I do if there is an error in curl file upload in php7?

Aug 13, 2021 am 09:20 AM

This article will introduce to you how to solve curl file upload errors in php7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What should I do if there is an error in curl file upload in php7?

Recently, when the project is connected to the material library interface of the WeChat public account, I use the curl post method to submit the material files, and I find that it keeps prompting

{"errcode":41005,"errmsg":"media data missing"}

Code content

$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);
Copy after login

After consulting the official documentation, @ is no longer supported after php5.5, it must be Use CurlFile or set CURLOPT_SAFE_UPLOAD to 1

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

In php7 curl, if you change CURLOPT_SAFE_UPLOAD, an error will be prompted as follows:

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

We can only use CurlFile to handle it honestly

$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);
Copy after login

And then found three big pitfalls in writing like this (I am stupid)

1. If CURLOPT_POSTFILEDS is passed in If it is an array, the content_type is multipart/form-data; if CURLOPT_POSTFILEDS passes in json or key-value& content_type, it is x-www-form_urlencoded; but WeChat supports the array passed by form-data

2. Inside the array If there is an object containing it, http_build_query will change it into an array

3. CurlFile can only read the path in the server. If you want to upload an address on the Internet, you need to download it to the temporary directory of the server first, and then pass CurlFile reads the file path (absolute path)

So we then adjust the code

$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);
Copy after login

Just when I thought I could get rid of it, a notice syntax error popped up here in php7:

Array to string conversion

Then I checked the information and found that CURLOPT_POSTFIEDLDS does not support multi-dimensional arrays

But the syntax error in the notice prompted, we can completely block it

Continue to adjust the code

$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);
Copy after login

In the end, the material was uploaded successfully

When I looked up, it was already dark

I was so happy that I quickly wiped my nose and packed my things to get off work

Recommended learning: php video tutorial

The above is the detailed content of What should I do if there is an error in curl file upload in php7?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)