search
HomeBackend DevelopmentPHP Tutorial请教一个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
谢谢
Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),