我是curl的新手,已经尝试了很多教程。最后一次尝试是使用以下代码:
<?php
// INIT CURL
$url="";
function login($url) {
$result = "";
$cookiePath = "/tmp/cookies.txt";
// Initialize the page
$page = curl_init();
// Set some options
curl_setopt($page, CURLOPT_COOKIESESSION, true);
curl_setopt($page, CURLOPT_REFERER, 'http://www.justanswer.com/login.aspx');
curl_setopt($page, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($page, CURLOPT_FRESH_CONNECT, true);
curl_setopt($page, CURLOPT_HEADER, true);
curl_setopt($page, CURLOPT_POST, true);
curl_setopt($page, CURLOPT_RETURNTRANSFER, true);
curl_setopt($page, CURLOPT_CONNECTTIMEOUT, 30);
$pattern = "#Set-Cookie: (.*?; path=.*?;.*?)'n#";
preg_match_all($pattern, $result, $matches);
array_shift($matches);
$cookie = implode("'n", $matches[0]);
// Use the parsed cookies with the next request
curl_setopt($page, CURLOPT_COOKIE, $cookie);
curl_setopt($page, CURLOPT_COOKIESESSION, true);
curl_setopt($page, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($page, CURLOPT_FRESH_CONNECT, true);
curl_setopt($page, CURLOPT_HEADER, false);
curl_setopt($page, CURLOPT_POST, false);
// Set the URL
curl_setopt($page, CURLOPT_URL, $url);
// Set the location of and send the cookies
curl_setopt($page, CURLOPT_COOKIEJAR, $cookiePath);
curl_setopt($page, CURLOPT_COOKIEFILE, $cookiePath);
// Send out POST data
curl_setopt($page, CURLOPT_POST, 1);
curl_setopt($page, CURLOPT_POSTFIELDS, "__EVENTTARGET=&__EVENTARGUMENT=&__SSVIEWSTATE=18&__VIEWSTATE=&email=****&sign_in_type=returning&username=&password=****&remember=true&ctl00%24BodyContent%24btnLogin.x=99&ctl00%24BodyContent%24btnLogin.y=17&SC_SID=346833306&SC_GUID=d157264a-24e2-47c0-b545-259ea5298e19&JS_BOOL=true&__EVENTVALIDATION=%2FwEWAwL%2BraDpAgKUnpuXBgKp%2F%2BSDCG4uM7n2SYrFbuk10%2FLkC64jclbvH6dXKmxM06VHN92G");
// Get the page
return $page;
}
$page = login($url);
// Go to the page and log in
curl_exec($page);
// Change the URL to the main page to check to see if the user is logged in
curl_setopt($page, CURLOPT_URL, "");
// Print the output
print curl_exec($page);
?>
请有人解释我做错了什么,我在firefox上使用LIVE HTTP头扩展复制了相同的POST请求,并且成功了。
登录页面在http://www.justanswer.com/login.aspx
请建议
TIA
尝试将表单变量包装为数组,例如:
$postVars = array("__EVENTTARGET" => "",
"__EVENTARGUMENT" => "",
"__SSVIEWSTATE" => "18",
"__VIEWSTATE" => "",
"email" => "****",
"sign_in_type" => "returning"
"foo" => "bar");