Q . How to send PHP cURL request and do not wait for the response?

Mike Tyson. asked, Sep 08 ' 2017

I need a PHP cURL configuration so that my script is able to send requests and ignore the answers sent by the API.

<?php

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
$result = curl_exec($ch);
echo $result;
curl_close ($ch);

?>

I tried adding:

//curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
//curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);

But its not working properly and the API webserver is not receiving the requests.

The reason for this is I am sending large amount of requests to the API therefore my script is very slow because it waits for each and every request.

Any help is appreciated.

  • cURL
  • asynchronous

1 Answers

Try This :

$url = 'http://examplesite.com/test/api/2';
$ch = curl_init();                
$post['test'] = 'test';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, TRUE);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); 

curl_setopt($ch, CURLOPT_USERAGENT, 'api');
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,  CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10); 

curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

$data = curl_exec($ch);   
echo $data;

curl_close($ch);

 

Leave a Ansewer