Make WordPress Core

Opened 4 months ago

#63828 new enhancement

Can't use `CURLFile` with `wp_remote_post`

Reported by: okvee's profile okvee Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.8.2
Component: HTTP API Keywords:
Focuses: Cc:

Description

Example code on WordPress plugin:

<?php
$imageFile = 'image.jpg';
$postFields = [];

$Finfo = new finfo();
$fileMimeType = $Finfo->file($imageFile, FILEINFO_MIME_TYPE);
unset($Finfo);

$CurlFile = new CURLFile($imageFile, $fileMimeType, 'curl-upload-' . basename($imageFile));
$postFields['image'] = $CurlFile;
unset($CurlFile, $fileMimeType);

$args = [
    'body' => $postFields,
    'sslverify' => false, // for self-signed localhost.
];
wp_remote_post('https://localhost/myupload.php', $args);

And on myupload.php file. I use few code to show how it work only.

<?php
move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/new-image.jpg');
$logs = print_r($_POST, true) . PHP_EOL .
    print_r($_FILES, true) . PHP_EOL;
file_put_content(__DIR__ . '/log.txt', $logs);

The file data will be send as text to destination (myupload.php).

This is because of these lines in [wp-includes/Requests/src/Transport/Curl.php](https://github.com/WordPress/wordpress-develop/blob/c726220a21d13fdb5409372b652c9460c59ce1db/src/wp-includes/Requests/src/Transport/Curl.php#L387C1-L396C4) inside method setup_handle() convert POST body to string.
So, CURLFile is dead and not working.

Compare to native PHP cURL functions. It is working fine.

<?php
$ch = curl_init('https://localhost/myupload.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// disable SSL verify. this is for test with self signed (local host) only.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

$response = curl_exec($ch);
curl_close($ch);

Please add an option, maybe a hook to skip convert POST data to let CURLFile works.

Change History (0)

Note: See TracTickets for help on using tickets.