• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1052
  • Last Modified:

Fill in form with curl.

I need to fill in a form using curl, the form has 2 file upload fields and normal text inputs. I need to fill in this form and submit using curl, then place the resulting page into a variable.

Thanks In advance.
  • 2
1 Solution
Marcus BointonCommented:
Does that include the file uploads too i.e. do you have some files you want to upload?
jimmyjoeAuthor Commented:
Marcus BointonCommented:
This function wraps it up very nicely (from http://melbourne.ug.php.net/content/view/97/76/):

function postRequest($cookie_file, $post_values, $url)
  // this is where we will store HTML output
  $data = null;

  // initialise cURL resource
  $cURL_resource = curl_init();

  curl_setopt($cURL_resource, CURLOPT_POSTFIELDS,     $post_values);
  curl_setopt($cURL_resource, CURLOPT_COOKIEFILE,     $cookie_file);
  curl_setopt($cURL_resource, CURLOPT_COOKIEJAR,      $cookie_file);
  curl_setopt($cURL_resource, CURLOPT_URL,            $url);
  curl_setopt($cURL_resource, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($cURL_resource, CURLOPT_RETURNTRANSFER, true);
  // true when debugging
  curl_setopt($cURL_resource, CURLOPT_HEADER,         false);

  // execute request
  $data = curl_exec($cURL_resource);
  // close cURL resource

  return $data;
} // END function postRequest($cookie_file, $post_values, $url)

To use it, it's just a matter of building a suitable POSTFIELDS array and calling the function. Obviously you'll need to amend the field names to match the form you're calling:

$postdata = array();
$postdata['file1'] = '@./uploads/file1.txt'; //The @ in front makes CURL load the file content, not just use the literal string
$postdata['file2'] = '@./uploads/file2.txt';
$postdata['firstname'] = urlencode('Jimmy'); //Some text field - note that you need to apply urlencoding
$postdata['email'] = urlencode('jimmy@example.com');
$results = postRequest('./cookies.txt', $postdata, 'http://www.example.com/formthing.php');
echo $results;

This is also mentioned in the curl docs user notes: http://www.php.net/manual/en/ref.curl.php

I can't find official documentation on the use of @, but it seems to work.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Suqinky {http:#13780201}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now