How to Send POST Request Without Waiting for Response?

  2390 views   3 months ago PHP

In this article, I will share with you how to send POST cURL requests without waiting for a response with an example. 

Currently, I was work with one big laravel application and in this application, we need to send webhook POST data on the merchant's notification POST url. before I write POST webhook which waiting response from the merchant side. so, it takes more time to execute. it's ok with the small amount of data. but what happened if you play with a big amount of data? then this logic not working and your application should not be working fast. so, i search on google and make a very simple solution for it. so, I share with you how to make a fire and forget POST requests in cURL.

Example :

function postCurlRequest(string $url, array $post_array, $check_ssl=true) {
    $cmd = "curl -L -X POST -H 'Content-Type: application/json'";
    $cmd.= " -d '" . json_encode($post_array) . "' '" . $url . "'";
  
    if (!$check_ssl){
      $cmd.= "'  --insecure"; // this can speed things up, though it's not secure
    }
    $cmd .= " > /dev/null 2>&1 &"; // don't wait for response

    // echo $cmd;die;

    exec($cmd, $output, $exit);
    return $exit == 0;
}

$data = [
	'order_no' => date('Y').rand(11111,99999).time(),
	'product_name' => 'products-1',
	'price' => 100,
	'currency' => 'USD',
];

$url = 'https://your_post_url.com';

$response = postCurlRequest($url, $data);

var_dump($response);

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles