• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1024
  • 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.
0
jimmyjoe
Asked:
jimmyjoe
  • 2
1 Solution
 
Marcus BointonCommented:
Does that include the file uploads too i.e. do you have some files you want to upload?
0
 
jimmyjoeAuthor Commented:
Yes.
0
 
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
  curl_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.
0
 
hujiCommented:
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.

Huji
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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