Phoenix
Phoenix

Reputation: 1075

How to handle this redirect using cURL

Here is my code:

echo post_data("http://www.ifreewind.net/iFreeWind.aspx",
               "__VIEWSTATE=%2FwEPDwULLTE3NjQ3MDc3NDQPZBYCAgMPZBYCAgEPFgIeB1Zpc2libGVoZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUSUmVtZW1iZXJNZUNoZWNrQm94r57YdIUtbSps%2FGLW1PUtjxcILdE%3D&__EVENTVALIDATION=%2FwEWBQLKivfjBgLw2N3fDgLC9%2FChAwLxuKbKAgL%2BjNCfDwU6DJjH4Q2acTlGVXmDrSv2Nn4G&UserNameTextBox=myemailaddress%40gmail.com&PasswordTextBox=mypassword&LoginButton=%E7%99%BB%E9%99%86");

function post_data($site, $data){
    $datapost = curl_init();
    $headers = array("Expect:");
    curl_setopt($datapost, CURLOPT_URL, $site);
    curl_setopt($datapost, CURLOPT_TIMEOUT, 40000);
    curl_setopt($datapost, CURLOPT_HEADER, TRUE);
    curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($datapost, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($datapost, CURLOPT_POST, TRUE);
    curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
    curl_setopt($datapost, CURLOPT_COOKIEFILE, "cookie.txt");
    ob_start();
    return curl_exec($datapost);
    ob_end_clean();
    curl_close($datapost);
    unset($datapost);
}

The second parameter of

post_data()
function, is the result I using Httpfox the firefox plugin generated. Those code doesn't work because this address automatically redirect to
http://www.ifreewind.net/Users/Index.aspx
, so I failed, please help me to pass the authorification.

Upvotes: 1

Views: 191

Answers (2)

minopret
minopret

Reputation: 4806

curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, true);

Upvotes: 1

PiTheNumber
PiTheNumber

Reputation: 23542

try: CURLINFO_EFFECTIVE_URL - Last effective URL

curl_setopt($datapost, CURLINFO_EFFECTIVE_URL, TRUE);

Upvotes: 0

Related Questions