0

I'm using cURL to submit a form on another webpage and have ran into a weird phenomenon:

When I use print_r($fields) to print out the post fields BEFORE I pass the array to cURL request and submit it, the form is submitted twice. When I remove that line from the script, the double submission disappears.

Why is printing the array causing a double submission?

//set POST variables
$fields = array();
$fields['input-name'] = 'input-value';

print_r($fields); //causes double submission

//open connection
$url = 'http://blah.com/blah/blah';
$request = curl_init($url);

//send post data
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $fields);

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
9
  • Quick question, how did you tell that it was submitting twice? Commented Oct 9, 2017 at 2:25
  • @Ibu I have access to the database that the other form puts submission data into. Commented Oct 9, 2017 at 2:29
  • I wonder why did you set CURLOPT_RETURNTRANSFER to TRUE, only to echo the return yourself? I searched your problem, and changing this helped in some ocasions. (no explanation to the double submital) php curl script run twice when printing return string Commented Oct 9, 2017 at 3:55
  • print_r doesn't affect your form submission for short. Commented Oct 9, 2017 at 4:13
  • @OctavioGalindo PHP's official documentation states that curl_exec "Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. " So an echo still seems necessary, and it's not printing my results twice. Thanks for looking that up! However, I already ran across that one. The only thing that has fixed my double submission issue is removing print_r($fields);. Commented Oct 9, 2017 at 13:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.