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:
<?php
curl_setopt($curl, CURLOPT_POST, 1);
?>
Now we need to specify the data to be transmitted. It can be contained as an array:
<?php
$data = ['field1'=>'value1', 'field2'=>'value2'];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
?>
Data can also be specified as a Query String:
<?php
$data = 'field1=value1&field2=value2';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
?>
Send a request to the following page and get the result:
<?php
if (!empty($_POST)) {
echo json_encode($_POST);
} else {
echo 'error';
}
?>
Send a request to the following page and get the result:
<?php
if (!empty($_POST)) {
echo $_POST['num1'] + $_POST['num2'];
} else {
echo 'error';
}
?>
Modify your function so that as a second optional parameter it accepts an array of data sent via the POST method.