Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Submitting a form via curl

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 188
    Comment on it

    Hers's is the tutorial to submit form via curl.

    I am taking example of log in form for say xyz.com site.

    $ch = curl_init();
    if($ch){
       $url = 'http://www.xyz.com/logmein.php';
        $postdata = array('LoginButton' => "Logon",
                           'PasswordTextbox'   => "mypasswordsafest",
                           'UsernameTextbox'   => "memyselfirine",
                           'secret_string' => "shhhmysecretstring"
                         );
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);   
    
        curl_exec($ch);
        $headers = curl_getinfo($ch);
        $errors = curl_error($ch);
        if($headers['http_code'] != 200) { 
                /* Error Code here */
        }else {
               /* do whatever */
        }
    }
    

    Let me explain what curl options do (after initializing)

    curl_setopt($ch, CURLOPT_URL, $url);
    

    setup the url you want to load, in this case we have login page url, you can also set this target url in curl_init function.

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    

    You dont want to keep curl script waiting for indefinite time, here 15 indicates a 15 sec period.

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    Redirects the action mentioned in the header after submission

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

    Do not display the output but return it as an string.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    

    Send data as post field

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    

    You need to send username and password atleast, send it in form of array, here I used $postdata array for this.

    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    

    Set the file where cookie should be stored

     curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
    

    A separate setting for curl to send cookie back to the server

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1');
    

    The Value you want to appear in the weblogs

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    

    Set this to false, if you want your request not to trigger in case of invalid certificate

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    

    Set this to false , if you want your request not to trigger in case of name in the remote server's certificate

    check these params and execute curl via curl_exec($ch);

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: