php中curl模拟登陆用户百度知道的例子

最近弄了一个工具,希望能获取自己百度网盘里面的数据但又不想公开数据,于是想到了模拟登陆百度,用常规的模拟登陆测试了下发现不行,抓取登陆时的数据才发现,其实百度登陆过程中跳转了几次页面,如果仅仅对http://passport.baidu.com/v2/api/?login一个页面获取cookie是不完整的那样就只有BAIDUID的值,而仅仅这个cookie值是没有多少作用的.

通过对抓包数据的分析,实际登陆过程中是先请求了一次http://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true这个页面,服务器同时给浏览器设置两个cookie,一个BAIDUID的cookie值,这个应该是与seesion id相关的;另一个是

Set-Cookie:

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

推测这个应该是百度检测浏览器是否支持cookie;

再次请求该页面,获取网页数据会得到一个token值用于登陆;

然后登陆成功会得到BDUSS等相关的cookie值,以上才是登陆成功,记录下上面的cookie即可!

下面是简单的请求及登陆函数集合,作为基础类吧,可能简单了点,以后再完善吧!

代码如下:

  1. <?php
  2. /**
  3. * 百度基础类
  4. * @author qaulau@hotmail.com
  5. * @file baidu.php
  6. * @date 2013-6-2 www.phpfensi.com
  7. */
  8. class baidu{
  9. private $cookie = '';
  10. private $username = '';
  11. private $password = '';
  12. const COOKIE_DIR = 'temp'; //cookie存放目录
  13. const COOKIE_VALIDATE = 604800; //cookie有效期,默认为7天
  14. const SECRET_KEY = 'hAFS6as8askNBVSuiealkkw'; //密钥用于加密cookie文件名,防止保存的cookie路径被猜测
  15. private function http_request($url, $post_data, $referef,$header = true){
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. if ($post_data != ""){
  19. curl_setopt($ch, CURLOPT_POST, 1);
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  21. }
  22. if ($referef != ""){
  23. curl_setopt($ch, CURLOPT_REFERER, $referef);
  24. }
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  27. curl_setopt($ch, CURLOPT_HEADER, $header);
  28. 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");
  29. if ($this->cookie != ""){
  30. curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
  31. }
  32. $data = curl_exec($ch);
  33. curl_close($ch);
  34. if ($header){
  35. preg_match_all('/Set-Cookie:((.+)=(.+))$/m ', $data, $cookies);
  36. if(is_array($cookies) && count($cookies) > 1 && count($cookies[1]) > 0){
  37. foreach($cookies[1] as $i => $k){
  38. $cookieinfos = explode(";", $k);
  39. if(is_array($cookieinfos) && count($cookieinfos) > 1){
  40. $this->cookie .= $cookieinfos[0];
  41. $this->cookie .= "; ";
  42. }
  43. }
  44. }
  45. }
  46. return $data;
  47. }
  48. private function login(){
  49. //生成一个cookie
  50. $ret = $this->http_request("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true", "", "");
  51. //获取token并保存cookie
  52. $ret = $this->http_request("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true", "", "");
  53. preg_match_all('/login_token='(.+)'/', $ret, $tokens);
  54. $login_token = $tokens[1][0];
  55. //登陆并保存cookie
  56. $post_data = array();
  57. $post_data['username'] = $this->username;
  58. $post_data['password'] = $this->password;
  59. $post_data['token'] = $login_token;
  60. $post_data['charset'] = "UTF-8";
  61. $post_data['callback'] = "parent.bd12Pass.api.login._postCallback";
  62. $post_data['index'] = "0";
  63. $post_data['isPhone'] = "false";
  64. $post_data['mem_pass'] = "on";
  65. $post_data['loginType'] = "1";
  66. $post_data['safeflg'] = "0";
  67. $post_data['staticpage'] = "https://passport.baidu.com/v2Jump.html";
  68. $post_data['tpl'] = "mn";
  69. $post_data['u'] = "http://www.baidu.com/";
  70. $post_data['verifycode'] = "";
  71. $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");
  72. //记录下所有cookie
  73. $this->writeCookie();
  74. }
  75. private function writeCookie(){
  76. if(!file_exists(self::COOKIE_DIR)){
  77. @mkdir(self::COOKIE_DIR) && touch(self::COOKIE_DIR.'/index.html');
  78. }
  79. $filename = self::COOKIE_DIR.'/'.md5($this->username.self::SECRET_KEY);
  80. file_put_contents($filename, $this->cookie);
  81. }
  82. public function baidu($username,$password){
  83. $this->username = $username;
  84. $this->password = $password;
  85. $filename = self::COOKIE_DIR.'/'.md5($this->username.self::SECRET_KEY);
  86. if ((@filemtime($filename)+ self::COOKIE_VALIDATE > time()) && ($cookie = file_get_contents($filename))!= ''){
  87. //如果cookie在有效期内且不为空
  88. $this->cookie = $cookie;
  89. }else {
  90. $this->login();
  91. }
  92. }
  93. /** www.phpfensi.com
  94. * 请求页面
  95. * @param string $url :页面地址
  96. * @param string $referef :引用页面
  97. * @param string $post_data :post数据,如果填写则为post方式否则为get方式
  98. * 返回页面数据
  99. */
  100. public function request($url,$referef = '',$post_data = ''){
  101. return $this->http_request($url,$referef,$post_data,false);
  102. }
  103. }

这个只是基本的类,只涉及登陆及请求与提交数据,可在此基础上使用,例如请求百度云网盘,代码如下:

  1. $baidu = new baidu('用户名','密码');
  2. $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');
  3. echo $data;

还可以用来作为贴吧的发帖或百度空间更新工具.