Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请教一个curl登录网站的问题

请教一个curl登录网站的问题

Jun 20, 2016 pm 12:38 PM

请教各位大神,我利用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;
Copy after login

========================================================

这里的思路是,我首先用(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;
Copy after login


从代码可以看出,我也是先打开第一个页面,设置cookie,并且拿到验证码图片的链接并在自己的网页上显示,人工进行识别并输入,然后点击自己的提交数据 post 到 login.php ,也就是1楼的代码里面去。

那你就跟踪一下,看看那个环节出了问题

那你就跟踪一下,看看那个环节出了问题



能具体讲讲怎么调试跟踪么?没搞过这块,经验为0
谢谢
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles