Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using curl to submit/retrieve a forms results

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.

like image 467
Jason Avatar asked Jan 03 '11 22:01

Jason


People also ask

How do I submit a form using curl?

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.

How do you send post form data?

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.

What does Curl_exec return?

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);


2 Answers

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!

like image 178
dqhendricks Avatar answered Sep 21 '22 07:09

dqhendricks


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.

like image 44
Luke Stevenson Avatar answered Sep 19 '22 07:09

Luke Stevenson