Forum Moderators: coopster
//setup Curl
$cookiename = substr($from,4,5);
$cookiefile = $cookiename . ".txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (Windows; MSIE 6.0; U; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
//read login page
curl_setopt($ch, CURLOPT_URL, "Login.aspx");
$result = curl_exec ($ch);
echo $result;
// extract values for hidden form fields __REQUESTDIGEST __VIEWSTATE __EVENTVALIDATION fields
//extract __REQUESTDIGEST
$start = strpos($result,"id=\"__REQUESTDIGEST\" value=\"") + 28;
$end = $start + 157;
$rdigest = substr($result , $start , $end - $start );
//extract __VIEWSTATE
$start = strpos($result,"id=\"__VIEWSTATE\" value=\"") + 24;
$end = $start + 16300;
$vstate = substr($result , $start , $end - $start );
$vstate = urlencode($vstate);
//extract __EVENTVALIDATION
$start = strpos($result,"id=\"__EVENTVALIDATION\" value=\"") + 30;
$end = $start + 120;
$event = substr($result , $start , $end - $start );
$event = urlencode($event);
//set login form values and login
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, 'Login.aspx');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '__REQUESTDIGEST=' . $rdigest . '&__VIEWSTATE=' . $vstate . '&__EVENTVALIDATION=' . $event . '&UserName=' . $from . '&Password=' . $password);
$result = curl_exec ($ch);
echo $result;
//extract __redirect
$start = strpos($result,"Location:") + 10;
$end = strpos($result,".aspx") +5;
$redirect = substr($result , $start , $end - $start );
$redirect = "https://www.domain.com/" . $redirect;
echo $redirect ."<br /><br />";
echo $result;
curl_setopt($ch, CURLOPT_URL, $redirect);
$result = curl_exec ($ch);
echo $result; Its just the one complete code extract with the header data it generates.
1. As coopster said CURLOPT_FOLLOWLOCATION needs to be on to follow redirects. Check your CURLOPT_MAXREDIRS default value.
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.
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.
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);].
5. The use of "Login.aspx" both in CURLOPT_URL and CURLOPT_REFERER can be an issue, although I'm not 100% sure.
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.