Sending Data Using POST Method via CURL in PHP

Using CURL, you can send data using the POST method, simulating a form submission. To do this, you need to specify that the request will be made using the POST method. This is done using the following setting:

curl.php<?php
  curl_setopt($curl, CURLOPT_POST, 1);
?>

Now we need to specify the data to be transmitted. It can be contained as an array:

curl.php<?php
  $data = ['field1'=>'value1', 'field2'=>'value2'];
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
?>

Data can also be specified as a Query String:

curl.php<?php
  $data = 'field1=value1&field2=value2';
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
?>
Задание 1

Send a request to the following page and get the result:

file.php<?php
  if (!empty($_POST)) {
    echo json_encode($_POST);
  } else {
    echo 'error';
  }
?>
Задание 2

Send a request to the following page and get the result:

file.php<?php
  if (!empty($_POST)) {
    echo $_POST['num1'] + $_POST['num2'];
  } else {
    echo 'error';
  }
?>
Задание 3

Modify your function so that as a second optional parameter it accepts an array of data sent via the POST method.