如何使用cURL处理此重定向

时间:2012-01-23 07:42:50

标签: php curl libcurl

这是我的代码:

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);
}

<{p>

post_data()
函数的第二个参数,是我使用Httpfox生成的firefox插件的结果。这些代码无效,因为此地址会自动重定向到
http://www.ifreewind.net/Users/Index.aspx
,所以我失败了,请帮我通过授权。

2 个答案:

答案 0 :(得分:1)

curl_setopt($ datapost,CURLOPT_FOLLOWLOCATION,true);

答案 1 :(得分:0)

尝试:CURLINFO_EFFECTIVE_URL - 上一个有效网址

curl_setopt($datapost, CURLINFO_EFFECTIVE_URL, TRUE);