Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a cUrl request without receiving the response?

Tags:

php

curl

Normally I Post data when I initiate cURL. And I wait for the response, parse it, etc...

I want to simply post data, and not wait for any response. In other words, can I send data to a Url, via cURL, and close my connection immediately? (not waiting for any response, or even to see if the url exists)

It's not a normal thing to ask, but I'm asking anyway.

Here's what I have so far:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $MyUrl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_send); 
curl_exec($ch);  
curl_close($ch);
like image 777
coffeemonitor Avatar asked Apr 03 '11 22:04

coffeemonitor


2 Answers

I believe the only way to not actually receive the whole response from the remote server is by using CURLOPT_WRITEFUNCTION. For example:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $MyUrl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_send); 
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'do_nothing');
curl_exec($ch);  
curl_close($ch);

function do_nothing($curl, $input) {
    return 0; // aborts transfer with an error
}

Important notes

  1. Be aware that this will generate a warning, as the transfer will be aborted.
  2. Make sure that you do not set the value of CURLOPT_RETURNTRANSFER, as this will interfere with the write callback.
like image 147
Jon Avatar answered Oct 12 '22 16:10

Jon


Not sure if this helps, but via command-line I suppose you could use the '--max-time' option - "Maximum time in seconds that you allow the whole operation to take."

I had to do something quick and dirty and didn't want to have to re-program code or wait for a response, so found the --max-time option in the curl manual

curl --max-time 1 URL

like image 44
Sparkacus Avatar answered Oct 12 '22 18:10

Sparkacus