PHP curl无法成功登录

时间:2011-01-27 07:55:34

标签: php curl login

我想登录以下网页,但我无法

http://www.hket.com/cas/login?service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check

以下是php curl代码,但无法成功登录

   $encode = "UTF-8";
     $agent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1 (.NET CLR 3.5.30729)";
     $cookieMobile = "cookieMobile.txt";    // need to use 2 different cookies since it will overwrite the old one when curl store cookie. cookie file is store under apache folder

 // submit a login
 function cLogin($url, $post, $agent, $cookiefile) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_COOKIESESSION, 1);  
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 20);   // follow the location if the web page refer to the other page automatically
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Get returned value as string (don’t put to screen)
  curl_setopt($ch, CURLOPT_USERAGENT, $agent);    // Spoof the user-agent to be the browser that the user is on (and accessing the php script)
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); // Use cookie.txt for STORING cookies
  curl_setopt($ch, CURLOPT_POST, true);       // Tell curl that we are posting data
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);   // Post the data in the array above
  $output = curl_exec($ch);  // execute
  curl_close($ch);

  return $output;
 }

 // mobile page login
 $url = "http://www.hket.com/cas/login?service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check";
 $post = "_spring_security_remember_me=true&username=abcdefg&password=123456&rememberMe=on&lt=e1s1&_eventId=submit";

 cLogin($url, $post, $agent, $cookieMobile);

我使用firefox登录,Live HTTP Headers提供以下信息:

我认为登录失败的原因是" HTTP / 1.1 302暂时移动"原因列在下面的消息

http://www.hket.com/cas/login

POST /cas/login HTTP/1.1
Host: www.hket.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.hket.com/cas/login?service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check
Cookie: JSESSIONID=0CED9E8EE4B45CF647D55B1D617C584E; __utma=240160410.1615466111.1278055560.1296028926.1296112093.36; __utmz=240160410.1296112093.36.31.utmcsr=abcdefg.myftp.org|utmccn=(referral)|utmcmd=referral|utmcct=/abc/; BIGipServerpool_http_hket_prod=2617354432.20480.0000; __utmc=240160410; __utmb=240160410.5.10.1296112093
Content-Type: application/x-www-form-urlencoded
Content-Length: 108
_spring_security_remember_me=true&username=abcdefg&password=123456&rememberMe=on&lt=e1s1&_eventId=submit

HTTP/1.1 302 Moved Temporarily
Date: Thu, 27 Jan 2011 07:38:19 GMT
Server: Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8b mod_jk/1.2.26 PHP/5.2.5
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Set-Cookie: CASPRIVACY=; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/cas
Set-Cookie: CASTGC=TGT-99869-nhDTBe4XNpmk7RmlHE1OIqZiOywPgiUfjphBadf4gK1IVOqZWN-cas; Expires=Thu, 28-Apr-2011 15:05:30 GMT; Path=/cas
Location: http://mobile.hket.com/mobile/j_spring_cas_security_check?ticket=ST-289544-CvNYVhenGTSVMqZI5Ida-cas
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain
----------------------------------------------------------
http://mobile.hket.com/mobile/j_spring_cas_security_check?ticket=ST-289544-CvNYVhenGTSVMqZI5Ida-cas

GET /mobile/j_spring_cas_security_check?ticket=ST-289544-CvNYVhenGTSVMqZI5Ida-cas HTTP/1.1
Host: mobile.hket.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.hket.com/cas/login?service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check
Cookie: JSESSIONID=41364FBEC5E18BED43EE8F4C68CDC876; __utma=240160410.1615466111.1278055560.1296028926.1296112093.36; __utmz=240160410.1296112093.36.31.utmcsr=abcdefg.myftp.org|utmccn=(referral)|utmcmd=referral|utmcct=/abc/; __utmc=240160410; BIGipServerpool_http_hketmobile_prod=2634131648.8225.0000; __utmb=240160410.5.10.1296112093

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://mobile.hket.com/mobile/
Content-Length: 0
Date: Thu, 27 Jan 2011 07:38:19 GMT
----------------------------------------------------------
http://mobile.hket.com/mobile/

GET /mobile/ HTTP/1.1
Host: mobile.hket.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.hket.com/cas/login?service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check
Cookie: JSESSIONID=41364FBEC5E18BED43EE8F4C68CDC876; __utma=240160410.1615466111.1278055560.1296028926.1296112093.36; __utmz=240160410.1296112093.36.31.utmcsr=abcdefg.myftp.org|utmccn=(referral)|utmcmd=referral|utmcct=/abc/; __utmc=240160410; BIGipServerpool_http_hketmobile_prod=2634131648.8225.0000; __utmb=240160410.5.10.1296112093

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Date: Thu, 27 Jan 2011 07:38:19 GMT
----------------------------------------------------------
http://toolbarqueries.google.com.hk/tbr?features=WH&sourceid=navclient-ff&client=navclient-auto-ff&googleip=O;218.189.25.83;56&swwk=-1&ch=878db86f2&q=info:http%3A%2F%2Fmobile.hket.com%2Fmobile%2F

GET /tbr?features=WH&sourceid=navclient-ff&client=navclient-auto-ff&googleip=O;218.189.25.83;56&swwk=-1&ch=878db86f2&q=info:http%3A%2F%2Fmobile.hket.com%2Fmobile%2F HTTP/1.1
Host: toolbarqueries.google.com.hk
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1 (.NET CLR 3.5.30729) GoogleToolbarFF 7.1.20101113
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: PREF=ID=e873609e1d8c2fd1:U=943830ece3685904:FF=1:LD=en:NR=50:TM=1277941596:LM=1294985111:DV=wf1QTY4G654M:S=3XqpPzdDwh1BlAxK; NID=43=iWMTmWA3E74L8quRFJXxuVLKz7UUKyKygfUrgHkEPoNCowgbOIDvxYURj67o_weqWXFoYlXsBqizFWrwQQK-HHVI1tVFuJK07cAizXOSM3YzuTEdpv85L8CcmVWXNmfM; SID=DQAAAMkAAACQie9hIy81BSvSY2OX3Ma0Bw2FF4OAfAX8HhzXyZpOu7NNlZs4vOWrpcf9tuPMoNhZgyyQukG9JvfUFxypgMxwnbDKNsBeHH0hCV0J1s7ailDkYaE6JcGlXWVhseWu0A3l9AbP8uUDUEcwGTF_saOFgDX-Rk3CB6_heNuuipI_QQW7c3svvkTm2HTVvho1kka3rRgPqvkYVTfYwdb9uw0cWQN7IfduIt_Sq-4mLjKVnakrJowAZdWBLzCHkLHiCXiUbOlBfPW8PsuSbcjPj4L_; HSID=AIkn5yoz2LP1K49rP

HTTP/1.1 200 OK
Date: Thu, 27 Jan 2011 07:38:19 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
Content-Length: 0
X-XSS-Protection: 1; mode=block
----------------------------------------------------------

如果我无法使用curl登录但是使用浏览器可以?

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试放

service=http%3A%2F%2Fmobile.hket.com%2Fmobile%2Fj_spring_cas_security_check

在你的$ post大肆宣传并压制'?'。

你用POST来解决你的卷曲问题,你确定你的目的地网址可以使用汉堡方法吗?如果不是,请尝试使用GET进行CURL。