Hey, I have figured out the answer if anyone is interested. It is not always possible to find the variables by fetching them from the website source. Sometimes they are generated on the server side, or it might be done on the client side in a complicated way to make it difficult to reproduce. Sometimes complicated forms are encoded and sent through the headers.
One answer is to use selenium firefox plugin and reverse engineer the actions done by the browser. You can record the actions and then export as php, java, C/C++ or whatever. This doesn't actually have any limitations, and it can automate any action done by a web browser.
PHP curl does have it's limitations, and the variables sent through the headers might change every time, so they can't be used for future automation.