Fill in form with curl.

Posted on 2005-04-12
Last Modified: 2012-06-27
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.
Question by:jimmyjoe
    LVL 25

    Expert Comment

    Does that include the file uploads too i.e. do you have some files you want to upload?

    Author Comment

    LVL 25

    Accepted Solution

    This function wraps it up very nicely (from

    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('');
    $results = postRequest('./cookies.txt', $postdata, '');
    echo $results;

    This is also mentioned in the curl docs user notes:

    I can't find official documentation on the use of @, but it seems to work.
    LVL 14

    Expert Comment

    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

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now