richelectron - 2:59 pm on Jan 7, 2011 (gmt 0)
1. As coopster said CURLOPT_FOLLOWLOCATION needs to be on to follow redirects. Check your CURLOPT_MAXREDIRS default value.
Okay, I've reverted to try and do this the automatic way. I did discover that I wasn't always getting in successfully because one of the hidden form fields was varying in length as well. And I had hard coded the length before, so I am managing to get the first page to load 100% of the time now.
2. I don't see how do you actually create $cookiefile. Be careful how you handle the open/read/write/close permissions. By using CURLOPT_COOKIEFILE you actually activate the curl cookie parser and curl will automatically handle all cookies in a single curl transaction WITHOUT such a file even exists! This is why your code might have worked with other websites, but is generating an error with this one.
For the purposes of testing I have hardcoded a cookiefile path and checked that it is being written to successfully.
3. It's possible to use multiple instances of CURLOPT_URL in one curl_exec transaction (as your case might be). However, curl's persistent connection capability can be used if ONLY all the URLs are on the same host! If you have a redirect for your second (PUT) request, I'm afraid, you have to use more than one curl_exec/curl_close transaction and store the cookies in between.
All of the URLS are on the same host.
4. To collect cookies received with your first (GET) request, set the CURLOPT_COOKIEJAR. Then use CURLOPT_COOKIEFILE in your second (PUT) curl transaction to recall them. But along with CURLOPT_POSTFIELDS you must use CURLOPT_POST. This is why you should uncomment [//curl_setopt($ch, CURLOPT_POST, true);].
I have enabled CURLOPT_POST
5. The use of "Login.aspx" both in CURLOPT_URL and CURLOPT_REFERER can be an issue, although I'm not 100% sure.
I've removed this referer value as well
6. Depending on configuration of the website under question and use of doPostBack functions, the use of CURLOPT_HTTPHEADER may be needed. You have to figure out what headers browser must send and create the respective array to be sent along with the PUT request.
Please could you possibly elaborate a bit more on this last point? Thanks.
Update: the initial page will load once I login, but if I try to navigate to any other pages then it prompts me for login again. I can see that the ASP.NET_SessionId cookie variable is being set automatically in my cookie file. But .ASPXAUTH is still not being picked up. Somehow I need to get this value, but I can't see it in the curl headers that are returned by default. The ASP.NET_SessionId Cookie value does show up in the headers though.