Ask a curl website logon question

Source: Internet
Author: User
Tags set cookie webp
Ask a curl user to log on to a website. if you fail to log on to a website using curl, could you kindly advise.

First, let's talk about the background of the application:
Http://my.tri.co.id/login/init,
This website is a telephone consultation Query website used to query the traffic of the current phone card. it is an Indonesian website.

I want to use php as a logon proxy for automatic query and summarization.

This website has a verification code during login, that is, the server uses session to establish a one-to-one correspondence between users and the identification code.

I used curl to implement raw data exactly the same as the browser post, but still failed to log on.

Browser 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

The raw stream simulated by curl:

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


However, the returned data is different, that is, the page returned by my program is not the page that is successfully logged on.

The browser returns:

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID = 5830130390f707d68d4e415557b9f054; Path =/
Location: http://my.tri.co.id/
Content-Language: en-US
Date: Tue, 26 Jan 2016 14:13:19 GMT
Content-Length: 0


Curl return:

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


My program is the implementation of login. php below:
========================================================== ============================
 * @ Link http://hightman.cn * @ copyright Copyright (c) 2015 Twomice Studio. * // set cookie file $ cookie_file = dirname (_ FILE __). '\ cookie.txt'; echo $ cookie_file."
"; $ 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."
"; $ Fields_string = substr ($ fields_string, 0, strlen ($ fields_string)-1); rtrim ($ fields_string," & "); print $ fields_string ."
"; // 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, success, 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); // use the cookies obtained above curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // use automatic jump curl_setopt ($ ch, CURLOPT_ENCODING ,""); // execute post $ result = curl_exec ($ ch); if (! $ Result) {$ error = curl_error (); echo $ error ."
";} Else {print" result length: ". strlen ($ result )."
";}$ Rinfo = curl_getinfo ($ ch); curl_close ($ ch); print $ result;

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

The idea here is that I first use (index. php: displays the logon interface.) curl obtains the cookie and logon ID image and submits the image to my own login. php. The source code of login. php is shown above to complete the logon.

However, I do not know why the failure is caused.

Thank you!


Reply to discussion (solution)

First, access the table single page to obtain the cookie variables JSESSIONID and BIGipServerSC_frontend_pool.
Access the verification code url (with the cookie variable) to obtain the cookie variable.
* *** There is manual intervention here. Therefore, you must save the cookie as a file and prevent sharing conflicts (preferably using the user ID as the file name)
Submit form data (including verification code) to the form target page (with cookie variable)

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string );
$ Fields_string can be an associated array and does not need to be assembled at will. If you want to use a string, you can also use the http_build_query function.

First, access the table single page to obtain the cookie variables JSESSIONID and BIGipServerSC_frontend_pool.
Access the verification code url (with the cookie variable) to obtain the cookie variable.
* *** There is manual intervention here. Therefore, you must save the cookie as a file and prevent sharing conflicts (preferably using the user ID as the file name)
Submit form data (including verification code) to the form target page (with cookie variable)

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string );
$ Fields_string can be an associated array and does not need to be assembled at will. If you want to use a string, you can also use the http_build_query function.



The cookie has been obtained in the processing of the previous page. It is used in login. php. I also checked the cookie data, and the session id data is the same. In addition, from the raw stream data, the same is true for all data.

However, login is incorrect. I don't know what you think about this phenomenon?

First, access the table single page to obtain the cookie variables JSESSIONID and BIGipServerSC_frontend_pool.
Access the verification code url (with the cookie variable) to obtain the cookie variable.
* *** There is manual intervention here. Therefore, you must save the cookie as a file and prevent sharing conflicts (preferably using the user ID as the file name)
Submit form data (including verification code) to the form target page (with cookie variable)

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string );
$ Fields_string can be an associated array and does not need to be assembled at will. If you want to use a string, you can also use the http_build_query function.



My processing process is your idea.

 * @ Link http://hightman.cn * @ Copyright Copyright (c) 2015 Twomice Studio. */header ('content-type: text/html; charset = utf-8 '); // prevents generated page garbled characters $ title = 'Technology Co., Ltd '; $ 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'; // first obtain cookies and save $ ch = curl_init ($ login_url); // initialize curl_setopt ($ ch, CURLOPT_HEADER, 0 ); // do not return the header part curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // return the string instead of directly outputting curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file ); // store 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 ($ token); echo $ img_link, PHP_EOL; echo $ session_id, PHP_EOL; $ fp = fopen ($ temp_file, "r"); // read-only open template $ str = fread ($ fp, filesize ($ temp_file )); // read the template content fclose ($ fp); $ str = str_replace ("{penglig_site_title}", $ title, $ str ); // replace content $ str = str_replace ("{img_url}", $ img_link, $ str); // replace content echo $ str;


As you can see from the code, I first open the first page, set the cookie, and get the link to the verification code Image and display it on my webpage. then, I manually identify and enter it, then click submit data to post to login. php, that is, the code on the first floor.

Then you can follow up and see if there is a problem at that stage.

Then you can follow up and see if there is a problem at that stage.



Can you tell me how to debug the tracing? I have never done this. experience is 0.
Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.