Методы выполнения POST-запросов с помощью cURL в PHP

Метод 1: базовый POST-запрос cURL

$url = 'https://example.com/api';
$data = array('key1' => 'value1', 'key2' => 'value2');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Метод 2: отправка данных JSON в POST-запросе

$url = 'https://example.com/api';
$data = json_encode(array('key1' => 'value1', 'key2' => 'value2'));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Метод 3. Добавление заголовков в POST-запрос

$url = 'https://example.com/api';
$data = array('key1' => 'value1', 'key2' => 'value2');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Метод 4. Отправка файлов в POST-запросе

$url = 'https://example.com/upload';
$file_path = '/path/to/file.jpg';
$data = array('file' => new CURLFile($file_path));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Это всего лишь несколько примеров использования cURL для выполнения POST-запросов в PHP. Не забудьте изменить URL-адреса, данные и заголовки в соответствии с вашими потребностями.