Post data to a url using CURL in PHP

Simple Code :-

<?php
# Our new data
$data = array(
 'user' => 2,
 'pass' => '2Test'
);
# Create a connection
$url = 'https://www.shivrajan.com/curl/get.php';
$ch = curl_init($url);
# Form data string
$postString = http_build_query($data, '', '&');
# Setting our options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Get the response
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);

?>

The Above code is able to post data without any error

2.Method 2 Little Biit Complex :-

<?php
function curlPost($url, $data, $headers = NULL) {
 $response = array();
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if(!empty($data)){
 //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
 }

if (!empty($headers)) {
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 }
 //some extra stuffs
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
 curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);
 
 
 $response[] = curl_exec($ch);

if (curl_error($ch)) {
 trigger_error('Curl Error:' . curl_error($ch));
 }
 $response[] = curl_getinfo($ch);
 $response[] = curl_errno($ch);
 $response[] = curl_error($ch);
 curl_close($ch);
 
 return $response;
}

$a = curlPost('https://www.shivrajan.com/curl/get.php', [
 'user' => 'admin',
 'pass' => '12345',
]);
var_dump($a);
?>

Backend Code Starts here (get.php)

<?php
if(isset($_POST['user']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];

$arr = array("user"=>$user, "pass"=>$pass);
$jsn = json_encode($arr);
file_put_contents('result.txt',$jsn);
echo $user."<br>".$pass;
}
else
{
 echo "nothing Received";
}
?>