Brad
Brad

Reputation: 12262

curl posting data multiple times for curl_setopt

$data = array();
$handle = curl_init($api_url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$result=curl_exec($handle);

I am too experienced with curl and debugging this code. The data gets posted successfully to the api url, but there is reports of the data being sent to the api 2 to 3 times each. I want to rule out the code above being the problem as I investigate potential a slow loading form after the user clicks submit, some users may click the submit button a couple times due to the form delay.

Do you see anything above that would reveal it being posted the same data two or three times?

Upvotes: 1

Views: 1297

Answers (1)

brandonscript
brandonscript

Reputation: 72965

Reviewing the OP code, it is a solid (and very simple) php curl expression.

The suggestion is to create a simple logger right before the curl is initiated (or in this case an echo).

This way, one can easily distinguish between the following:

  1. It's the php script is being called multiple times by the client, or
  2. It is the curl code looping somehow

If you see one log line for each dupe, the cause is 1. If you see only one log line for the entire set of dupes, then the cause is 2.

Upvotes: 2

Related Questions