请教一个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;
========================================================
这里的思路是,我首先用(index.php, 登录界面的呈现) curl 实现了 cookie和登录识别码图片的获取,然后 提交到我自己的login.php进行处理。login.php 的源码如上所示,完成登录。
但不知道是什么原因导致失败。
真心请教各位,谢谢!
回复讨论(解决方案)
先访问表单页,获取 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 已经在前一个页面的处理中拿到了。在login.php中就是使用。我也查看了cookie的数据,session id数据是一样的。另外从raw stream 的数据来看,所有的数据也是一样。
但是登录就是不对。不知道您对这个现象怎么看?
先访问表单页,获取 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;
从代码可以看出,我也是先打开第一个页面,设置cookie,并且拿到验证码图片的链接并在自己的网页上显示,人工进行识别并输入,然后点击自己的提交数据 post 到 login.php ,也就是1楼的代码里面去。
那你就跟踪一下,看看那个环节出了问题
那你就跟踪一下,看看那个环节出了问题
能具体讲讲怎么调试跟踪么?没搞过这块,经验为0
谢谢

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
