I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.
I created a simple form:
<form name="test" method="post" action="form.php">
<input type="text" name="name" size="40" />
<input type="text" name="comment" size="40" />
<input type="submit" value="submit" />
</form>
In addition, I have php code to handle this form in the same page. All it does is echo back the form values.
The curl that I have been using is this:
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "path/to/form.php");
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'name' => 'yes',
'comment' => 'no'
));
curl_setopt($h, CURLOPT_HEADER, false);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show the variables that PHP should have echo'd when the form is submitted.
would appreciate any help with this.
Thanks.
How to submit an HTML form using Curl? To post a web form with Curl, you need to use the -d command line option and pass the form data as key/value pairs. By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type.
To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.
From the manual: 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. Your code already contains this line (which is good): curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
AHHHH after your comment, the problem is clear.
you need to include one more POST variable in your array.
'submitted' => 'submitted'
the submit button also returns a post value if clicked, which you are checking in your form processing PHP.
if ($_POST['submitted']) {
in your curl code however you have left out the 'submitted' post variable.
if this is what you are looking for, please select the check mark next to this answer. thanks!
From reading the other two answers, and the comment by the OP, I have a couple of ideas.
Specifically, the OP Comment was:
Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it
Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:
array(
'name' => 'The Name as Entered in the Form' ,
'comment' => 'The Comment as Entered in the Form' ,
'submit' => 'submit' # From the "Submit" button
);
Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".
1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.
2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.
So, I would suggest that you:
1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.
2) Check your Basic Form, and ensure that it has the appropriate fields.
3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die();
at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With