|
<? 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 );
}
} |