Maison > Article > développement back-end > 请教一个curl登录网站的问题
请教各位大神,我利用curl登录一个网站的时候登录失败,能否麻烦大家指点一下。
首先说一下应用背景:
http://my.tri.co.id/login/init,
这个网站是一个电话咨询查询网站,用来查询当前电话卡的流量,是印尼网站。
我想利用php做一个登录代理,实现自动查询以及汇总。
这个网站在登录的时候有一个验证码,也就是服务器通过session来建立用户跟识别码的一一对应关系。
我用curl实现了跟浏览器的post完全相同的raw data,但是依然登录失败。
浏览器 raw stream:
721 bytes sent to 180.214.234.59:80
POST /login/submit HTTP/1.1
Host: my.tri.co.id
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://my.tri.co.id
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://my.tri.co.id/login/submit
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: BIGipServerSC_frontend_pool=123211018.37151.0000; JSESSIONID=BAD8502FE89E3929E468FCEB255DFA77
Content-Length: 68
modelName=login&msisdn=089506850651&password=indonesia&checkNum=6998
curl 模拟的 raw stream:
721 bytes sent to 180.214.234.59:80
POST /login/submit HTTP/1.1
Host: my.tri.co.id
Cookie: BIGipServerSC_frontend_pool=123211018.37407.0000; JSESSIONID=EE68C4C157664FB0E964FFB1A950968A
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://my.tri.co.id
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://my.tri.co.id/login/submit
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Content-Length: 68
modelName=login&msisdn=089506850651&password=indonesia&checkNum=4608
但是返回的数据却不同,也就是说我的程序返回的页面不是登陆后成功的页面。
浏览器返回:
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=5830130390F707D68D4E465557B9F054; Path=/
Location: http://my.tri.co.id/
Content-Language: en-US
Date: Tue, 26 Jan 2016 14:13:19 GMT
Content-Length: 0
curl返回:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Vary: Accept-Encoding
Date: Tue, 26 Jan 2016 16:09:49 GMT
Content-Length: 5569
我的程序是下面的实现 login.php:
==========================================================
<?php /** * Demo for the parallel HTTP client * * @author hightman <hightman@twomice.net> * @link http://hightman.cn * @copyright Copyright (c) 2015 Twomice Studio. */ // set cookie file $cookie_file = dirname(__FILE__) . '\cookie.txt'; echo $cookie_file . "<br />"; $usr_form_name = 'usr'; $pwd_form_name = 'pwd'; $check_num_form_name = 'check_num'; $post_url = 'http://my.tri.co.id/login/submit'; $usr = $_POST[$usr_form_name]; $pwd = $_POST[$pwd_form_name]; $check_num = $_POST[$check_num_form_name]; //echo $usr . " " . $pwd . " " .$check_num; $tri_usr_form_name = 'msisdn'; $tri_pwd_form_name = 'password'; $tri_check_num_form_name = 'checkNum'; $tri_mode_form_name = 'modelName'; $fields = array( $tri_mode_form_name => urlencode('login'), $tri_usr_form_name => urlencode($usr), $tri_pwd_form_name => urlencode($pwd), $tri_check_num_form_name => urlencode($check_num) ); //url-ify the data for the POST $fields_string = ''; foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value . '&'; } print $fields_string . "<br />"; $fields_string = substr($fields_string, 0, strlen($fields_string)-1); rtrim($fields_string, "&"); print $fields_string . "<br />"; //open connection $ch = curl_init(); $header[] = "Connection: keep-alive"; $header[] = "Cache-Control: max-age=0"; $header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; $header[] = "Origin: http://my.tri.co.id"; $header[] = "Upgrade-Insecure-Requests: 1"; $header[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"; $header[] = "Content-Type: application/x-www-form-urlencoded"; $header[] = "Referer: http://my.tri.co.id/login/submit"; $header[] = "Accept-Encoding: gzip, deflate"; $header[] = "Accept-Language: en-US,en;q=0.8"; curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $post_url); curl_setopt($ch, CURLOPT_REFERER, 'http://my.tri.co.id'); //curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch ,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($ch, CURLOPT_ENCODING, ""); //execute post $result = curl_exec($ch); if (!$result) { $error = curl_error(); echo $error . "<br />"; } else { print "result length: " . strlen($result) . "<br />"; } $rinfo=curl_getinfo($ch); curl_close($ch); print $result;
先访问表单页,获取 cookie 变量 JSESSIONID 和 BIGipServerSC_frontend_pool
再访问校验码url(带上cookie变量),获取 cookie 变量
***** 这里有人工介入,所以必须以文件方式保存 cookie,并防止共享冲突(最好是以用户id为文件名)
提交表单数据(含校验码)到表单目标页(带上cookie变量)
curl_setopt($ch ,CURLOPT_POSTFIELDS, $fields_string);
$fields_string 可以是关联数组,并不需要你费心组装。如果一定想用字符串,那也可以使用 http_build_query 函数
先访问表单页,获取 cookie 变量 JSESSIONID 和 BIGipServerSC_frontend_pool
再访问校验码url(带上cookie变量),获取 cookie 变量
***** 这里有人工介入,所以必须以文件方式保存 cookie,并防止共享冲突(最好是以用户id为文件名)
提交表单数据(含校验码)到表单目标页(带上cookie变量)
curl_setopt($ch ,CURLOPT_POSTFIELDS, $fields_string);
$fields_string 可以是关联数组,并不需要你费心组装。如果一定想用字符串,那也可以使用 http_build_query 函数
先访问表单页,获取 cookie 变量 JSESSIONID 和 BIGipServerSC_frontend_pool
再访问校验码url(带上cookie变量),获取 cookie 变量
***** 这里有人工介入,所以必须以文件方式保存 cookie,并防止共享冲突(最好是以用户id为文件名)
提交表单数据(含校验码)到表单目标页(带上cookie变量)
curl_setopt($ch ,CURLOPT_POSTFIELDS, $fields_string);
$fields_string 可以是关联数组,并不需要你费心组装。如果一定想用字符串,那也可以使用 http_build_query 函数
<?php/** * Demo for the parallel HTTP client * * @author hightman <hightman@twomice.net> * @link http://hightman.cn * @copyright Copyright (c) 2015 Twomice Studio. */header('content-type:text/html; charset=utf-8');//防止生成的页面乱码$title = '科技有限公司';$temp_file = 'template.html';$dest_file = 'login.html';// set cookie file$cookie_file = dirname(__FILE__) . '\cookie.txt';echo $cookie_file, PHP_EOL;//$http->setCookiePath($cookie_file);// simple load response contents$link_header = 'http://my.tri.co.id';$response_len = 0;$loop_times = 0;$login_url = $link_header . '/login/init';//先获取cookies并保存$ch = curl_init($login_url); //初始化curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //存储cookiescurl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);$response = curl_exec($ch);curl_close($ch);//echo number_format(strlen($response)) . ' bytes', PHP_EOL;//echo $response;$img_link_header = '/verifycode;jsessionid';$img_link_tail = '?sessionKey=loginCN&null';$header_pos = strpos($response, $img_link_header);$tail_pos = strpos($response, $img_link_tail, $header_pos) + strlen($img_link_tail);//echo 'header pos: ' . $header_pos, PHP_EOL;//echo 'tail pos: ' . $tail_pos, PHP_EOL;$img_link = $link_header . substr($response, $header_pos, $tail_pos - $header_pos);$jsession_id_header = 'jsessionid=';$session_key_header = '?sessionKey';$session_id_pos = strpos($img_link, $jsession_id_header);$session_key_pos = strpos($img_link, $session_key_header);$session_id = substr($img_link, $session_id_pos + strlen($jsession_id_header), $session_key_pos - $session_id_pos - strlen($jsession_id_header));echo $img_link, PHP_EOL;echo $session_id, PHP_EOL;$fp = fopen($temp_file, "r"); //只读打开模板$str = fread($fp, filesize($temp_file));//读取模板中内容fclose($fp);$str = str_replace("{penglig_site_title}", $title, $str);//替换内容$str = str_replace("{img_url}", $img_link, $str);//替换内容echo $str;
那你就跟踪一下,看看那个环节出了问题
那你就跟踪一下,看看那个环节出了问题