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?

醉折花枝作酒筹
Release: 2023-02-18 07:56:01
forward
2751 people have browsed it

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template