在 Extbase、TYPO3 CMS 中使用 http 请求上传文件

File upload using http request in Extbase, TYPO3 CMS

我们想使用 http 请求将文件上传到 TYPO3 9 中的某些 api 端点。

File upload can be done using \TYPO3\CMS\Core\Http\RequestFactory in TYPO3

$filePath = '/var/www/html/MyTypo3Project/Image.png';
$username = 'test';
$password = 'test';

$multipart = [
    // File Parameter
    [
        'name'     => 'Image', //Api side parameter name
        'contents' => fopen(realpath($filePath), 'r'), 
        'filename' => 'MyCustomName.png' // Custom filename
    ],

    //Other Parameters
    [
        'name'     => 'custom_param',
        'contents' => 'custom_param_value'
    ]
];

// Request options along with auth header
$additionalOptions = [
    'auth' => [$username, $password],
    'multipart' => $multipart,
];

$requestFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\RequestFactory::class);
$response = $requestFactory->request(
    $url, // Api Endpoint Url
    'POST', 
    $additionalOptions // Passing the additional options
);