Skip to content
iTecNote
  • Python
  • Javascript
  • PHP
  • Java
  • Android
  • iOS
  • jQuery
  • MySQL

Login POST form with cURL

curllogin

I am trying to login to the website www.centralgreen.com.sg/login.php using cURL (first time user of cURL)

I used the Tamper plugin for Firefox, and I have 4 POST inputs:

login=mylogin
password=mypassword
Button_DoLogin.x=33
Button_DoLogin.y=6

I tried to use curl again using

curl.exe --data 'login=mylogin&password=mypassword&Button_DoLogin.x=33&Button_DoLogin.y=6' www.centralgreen.com.sg/login.php?ccsForm=Login

But the login apparently doesn't go through (the output of this command is the same form, filled with only the password, and an error message The value in field Username is required.)

Here is the full list of info I get from Tamper

Host centralgreen.com.sg
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept Language en-us,en;q=0.5
Accept Encoding gzip, deflate
Accept Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep Alive 115
Connection keep-alive
Referer http://centralgreen.com.sg/login.php
Cookie PHPSESSID=65fbxxxxxxx02694e3a72xxxxxxxx; Valley_ParkLogin=04E203A459FCxxxxxxxA2DCD9AAE5B9678G08C04F1952155E2482xxxxxxxxxxxxx

Is there anything I do wrong? How can I pass the login through the POST form?

Best Solution

Try using the -F option instead of --data.

http://curl.haxx.se/docs/manpage.html#-F

Basically, this changes the content type header to:

Content-Type: multipart/form-data

This may give you a better result.

Related Solutions

How to send a header using a HTTP request through a cURL call

man curl:

   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H "Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they will only mess things up for you.

          See also the -A/--user-agent and -e/--referer options.

          This option can be used multiple times to add/replace/remove  multi-
          ple headers.

Example:

curl --header "X-MyHeader: 123" www.google.com

You can see the request that curl sent by adding the -v option.

Php – How to simulate browser form POST method using PHP/cURL

You've not posted a coherent/consistent stream of code. Is the bit at the end what you expect to happen? Or something else?

You say "it didn't work" - sorry, but we need a lot more information to be able to help you diagnose the problem.

  • Was there an error message?

  • What are you trying to post the file to?

  • Does the receiving URL work with an HTTP form?

  • Can you provide an example of the form it works with?

  • Do you control the code at the receiving end?

  • How do you know it "doesn't work"?

  • Do you get an error message? If so, what?

The operation should be as simple as:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $THE_REMOTE_URL_YOU_ARE_POSTING_TO);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    "field" => "@c:\\file_location.txt", // note the double \\ when used within double quotes
    'a_number' => 12345.
    'a_string' => "hello world"
  )); 
$response = curl_exec($ch);
?>

The bad path might exaplin why curl_getinfo() is not telling you what you expect to see - looking at the actual data exchange might be a lot more helpful. C.

Related Question
  • How to display request headers with command line curl
  • Php – Simulating a POST with PHP & cURL
  • Json – How to POST JSON data with cURL
  • PHP Curl – Cookies problem