API With POST Parameters in PHP
API parameters can be passed not only via the GET method but also via the POST method. Let's see how this is done. Let our API expect data via the POST method:
<?php
echo mt_rand($_POST['num1'], $_POST['num2']);
?>
Let's make a request to this API. For this, we will need the CURL library. Let's make a POST request using it:
<?php
$url = 'http://api.loc/index.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
$data = ['num1'=>'1', 'num2'=>'100'];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($curl);
var_dump($res);
?>
Create an API that will accept a zodiac sign and date via POST data, and return a horoscope for that sign on the specified date.