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

Source: Internet
Author: User
Tags curl md5

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: Copy code

<? Php
/**
* Baidu basic
* @ Author qaulau@hotmail.com
* @ File baidu. php
* @ Date 2013-6-2 www.111cn.net

*/

Class baidu {

Private $ cookie = '';
Private $ username = '';
Private $ password = '';
Const COOKIE_DIR = 'temp '; // cookie storage directory
Const COOKIE_VALIDATE = 604800; // cookie validity period. The default value is 7 days.
Const SECRET_KEY = 'hafs6as8asknbvsuiealkkw '; // The key is used to encrypt the cookie file name to prevent the cookie path from being guessed.

Private function http_request ($ url, $ post_data, $ referef, $ header = true ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );

If ($ post_data! = ""){
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
  }

If ($ referef! = ""){
Curl_setopt ($ ch, CURLOPT_REFERER, $ referef );
  }

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0 );
Curl_setopt ($ ch, CURLOPT_HEADER, $ header );
Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31 ");

If ($ this-> 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 ","","");

// Obtain 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 in 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: SECRET_KEY );
If (@ filemtime ($ filename) + self: COOKIE_VALIDATE> time () & ($ cookie = file_get_contents ($ filename ))! = ''){
// If the cookie is valid and is not empty
$ This-> cookie = $ cookie;
} Else {
$ This-> login ();
  }
 }

/** Www.111Cn.net
* Request page
* @ Param string $ url: page address
* @ Param string $ referef: Reference Page
* @ Param string $ post_data: post data. If this parameter is set, it is 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: Copy code

$ 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.