我需要登录并保存一些页面,但在登录后,保存cookie.txt会重定向回登录页面。看起来它没有登录。
这是我的代码:
<?php
$ch = curl_init();
$cookie_file_path = 'cookie.txt';
$cookie_file_path = realpath($cookie_file_path);
$data = array();
$data['ctl00$MainContent$txtUserName'] = "****";
$data['ctl00$MainContent$txtPassword'] = "****";
$postStr = "";
foreach($data as $key=>$d){
$postStr .= $key.'='.urlencode($d).'&';
}
$postStr = substr($postStr,0,-1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL,"http://member.phonhadat.com/dang-nhap.htm");
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postStr);
curl_exec ($ch); // execute the curl command
echo 'Curl error: ' . curl_error($ch); //no errrors
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL,"http://member.phonhadat.com/thay-doi-thong-tin.htm");
//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec ($ch);
echo 'Curl error: ' . curl_error($ch);
curl_close ($ch);
?>
我已阅读有关StackOverflow的所有问题 以下是我在cookie.txt中的内容:
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_member.phonhadat.com FALSE / FALSE 0 ASP.NET_SessionId imeoy32iizgq4cmbvtlcqwwl
以下是我卷曲错误的原因:
HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Access-Control-Allow-Origin: * Access-Control-Allow-Headers: cache-control, origin, x-requested-with, content-type Access-Control-Allow-Methods: POST Date: Fri, 15 Aug 2014 09:52:26 GMT Content-Length: 20399
Curl error: HTTP/1.1 **302 Found** Cache-Control: private Content-Type: text/html; charset=utf-8 Location: /dang-nhap.htm?returnurl=%2fthay-doi-thong-tin.htm Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Access-Control-Allow-Origin: * Access-Control-Allow-Headers: cache-control, origin, x-requested-with, content-type Access-Control-Allow-Methods: POST Date: Fri, 15 Aug 2014 09:52:26 GMT Content-Length: 167 HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Access-Control-Allow-Origin: * Access-Control-Allow-Headers: cache-control, origin, x-requested-with, content-type Access-Control-Allow-Methods: POST Date: Fri, 15 Aug 2014 09:52:26 GMT Content-Length: 20507
如果有人能帮我解决这个问题,我将非常感谢。