In php, the curl simulated login user Baidu Knows the example

Source: Internet
Author: User
Recently, I got a tool to obtain the data in my Baidu online storage, but I didn't want to disclose the data. so I thought of simulating a login to Baidu and testing it with a conventional simulated login, only data captured during login can be found. In fact, Baidu redirects several pages during the login process... recently, I got a tool to obtain the data in my Baidu online storage, but I didn't want to disclose the data. so I thought of simulating a login to Baidu and testing it with a conventional simulated login, capture login data only found, in fact, Baidu login process jump to the page several times, if only on the http://passport.baidu.com/v2/api? Login does not obtain complete cookies on a page, so only the value of BAIDUID is available.

Through the analysis of packet capture data, the actual login process is to request a http://passport.baidu.com/v2/api/ first? Getapi & class = login & tpl = mn & tangram = true on this page, the server sets two cookies for the browser and one cookie value for the BAIDUID, which should be related to the seesion id; the other is

Set-Cookie:

HOSUPPORT = 1; expires = Thu, 19-Aug-2021 15:41:37 GMT; path =/; domain = passport.baidu.com; httponly

Speculate that this should be Baidu's browser's support for cookies;

Request the page again and get the webpage data to get a token value for login;

After successful login, you will get related cookie values such as BDUSS. the above is the successful login. just record the above cookie!

The following is a simple set of requests and login functions. as a basic class, it may be a little simpler. Please complete it later!

The code is as follows:

 Cookie! = "") {Curl_setopt ($ ch, CURLOPT_COOKIE, $ this-> cookie);} $ data = curl_exec ($ ch); curl_close ($ ch); if ($ header) {preg_match_all ('/Set-Cookie :((. +) = (. +) $/M', $ data, $ cookies); if (is_array ($ cookies) & count ($ cookies)> 1 & count ($ cookies [1])> 0) {foreach ($ cookies [1] as $ I => $ k) {$ cookieinfos = explode (";", $ k); if (is_array ($ cookieinfos) & count ($ cookieinfos)> 1) {$ this-> cookie. = $ cookieinfos [0]; $ this-> cookie. = ";" ;}}}return $ data;} private function login () {// Generate a cookie $ ret = $ this-> http_request (" https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true "," "," "); // Get the token and save the cookie $ ret = $ this-> http_request (" https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true "," "," "); Preg_match_all ('/login_token = '(. +) '/', $ ret, $ tokens); $ login_token = $ tokens [1] [0]; // log on and save the cookie $ post_data = array (); $ post_data ['username'] = $ this-> username; $ post_data ['password'] = $ this-> password; $ post_data ['token'] = $ login_token; $ post_data ['charset'] = "UTF-8"; $ post_data ['callback'] = "parent. bd12Pass. api. login. _ postCallback "; $ post_data ['index'] =" 0 "; $ post_data ['isphone'] =" false "; $ post_data ['mem _ pass'] = "on"; $ post_data ['logintype'] = "1"; $ post_data ['safeflg'] = "0 "; $ post_data ['staticpage'] =" https://passport.baidu.com/v2Jump.html "; $ Post_data ['tpl'] =" mn "; $ post_data ['u'] =" http://www.baidu.com/ "; $ Post_data ['verifycode'] =" "; $ ret = $ this-> http_request (" http://passport.baidu.com/v2/api/?login ", $ Post_data ," https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F "); // Record all cookies $ this-> writeCookie ();} private function writeCookie () {if (! File_exists (self: COOKIE_DIR) {@ mkdir (self: COOKIE_DIR) & touch (self: COOKIE_DIR. '/index.html');} $ filename = self: COOKIE_DIR. '/'. md5 ($ this-> username. self: SECRET_KEY); file_put_contents ($ filename, $ this-> cookie);} public function baidu ($ username, $ password) {$ this-> username = $ username; $ this-> password = $ password; $ filename = self: COOKIE_DIR. '/'. md5 ($ this-> username. self: SEC RET_KEY); if (@ filemtime ($ filename) + self: COOKIE_VALIDATE> time () & ($ cookie = file_get_contents ($ filename ))! = '') {// If the cookie is valid and is not blank $ this-> cookie = $ cookie;} else {$ this-> login ();}} /** www.phprm.com * Request page * @ param string $ url: page address * @ param string $ referef: reference page * @ param string $ post_data: post data, if this parameter is set to post, or get * return page data */public function request ($ url, $ referef = '', $ post_data = '') {return $ this-> http_request ($ url, $ referef, $ post_data, false) ;}}?>

This is only a basic class that only involves login and request and submission of data. it can be used on this basis, for example, to request Baidu cloud network disk. the code is as follows:

$ Baidu = new baidu ('username', 'password'); $ data = $ baidu-> request ('http: // pan.baidu.com/api/list? Channel = chunlei & clienttype = 0 & web = 1 & num = 100 & page = 1 & dir = % 2F ', 'http: // pan.baidu.com'); echo $ data;

It can also be used as a post or Baidu Space update tool.


Tutorial address:

Reprinted! But please include the article address ^

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.