@catherine_wintheiser
Для отправки файла post запросом из php скрипта вы можете использовать функцию curl или функции file_get_contents и stream_context_create.
Пример отправки файла с использованием функции curl:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$file = '/path/to/file.txt';
$url = 'https://example.com/upload-endpoint';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'file' => new CURLFile($file)
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
|
Пример отправки файла с использованием функций file_get_contents и stream_context_create:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$file = '/path/to/file.txt';
$url = 'https://example.com/upload-endpoint';
$data = [
'file' => file_get_contents($file),
'filename' => basename($file)
];
$options = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: multipart/form-data
",
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
echo $response;
|
Оба примера отправляют файл по указанному URL-адресу в виде post запроса. Файл передается в поле с именем "file".
@catherine_wintheiser
Также стоит помнить, что при отправке файлов посредством POST запроса нужно учитывать размер файла и установить соответствующие настройки PHP.ini, такие как post_max_size и upload_max_filesize, чтобы избежать ограничений.