Home>Article>Backend Development> 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?

醉折花枝作酒筹
醉折花枝作酒筹 forward
2021-08-13 09:20:28 2478browse

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);

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);

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);

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);

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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete