前几天,先测试了利用Python脚本登录百度空间,问题已解决,可以参照我的博文《python 脚本登录百度空间》,今天完善了php方式的登录过程,原理同python脚本的登录。
代码如下:
登录代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | private function loginBaiduZone($user,$password) { //get Cookie $ret = $this->doHttpPost("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false", "", ""); //get token $ret = $this->doHttpPost("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false", "", ""); preg_match_all('/login_token=\'(.+)\'/', $ret, $tokens); $login_token = $tokens[1][0]; //login $post_data = array(); $post_data['username'] = $user; $post_data['password'] = $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->doHttpPost("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"); } |
private function loginBaiduZone($user,$password) { //get Cookie $ret = $this->doHttpPost("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false", "", ""); //get token $ret = $this->doHttpPost("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=false", "", ""); preg_match_all('/login_token=\'(.+)\'/', $ret, $tokens); $login_token = $tokens[1][0]; //login $post_data = array(); $post_data['username'] = $user; $post_data['password'] = $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->doHttpPost("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"); }
Http请求的工具函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | private function doHttpPost($url, $post_data, $referef) { $mcurl = curl_init(); curl_setopt($mcurl, CURLOPT_URL, $url); if ($post_data != "") { curl_setopt($mcurl, CURLOPT_POST, 1); curl_setopt($mcurl, CURLOPT_POSTFIELDS, $post_data); } if ($referef != "") { curl_setopt($mcurl, CURLOPT_REFERER, $referef); } curl_setopt($mcurl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($mcurl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($mcurl, CURLOPT_HEADER, 1); curl_setopt($mcurl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER"); if ($this->cookie != "") { curl_setopt($mcurl, CURLOPT_COOKIE, $this->cookie); } $data = curl_exec($mcurl); curl_close($mcurl); 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 doHttpPost($url, $post_data, $referef) { $mcurl = curl_init(); curl_setopt($mcurl, CURLOPT_URL, $url); if ($post_data != "") { curl_setopt($mcurl, CURLOPT_POST, 1); curl_setopt($mcurl, CURLOPT_POSTFIELDS, $post_data); } if ($referef != "") { curl_setopt($mcurl, CURLOPT_REFERER, $referef); } curl_setopt($mcurl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($mcurl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($mcurl, CURLOPT_HEADER, 1); curl_setopt($mcurl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER"); if ($this->cookie != "") { curl_setopt($mcurl, CURLOPT_COOKIE, $this->cookie); } $data = curl_exec($mcurl); curl_close($mcurl); 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; }
接下来再测试博文的发布代码。