首页 >php教程 >php手册 >PHP 登录百度空间

PHP 登录百度空间

WBOY
WBOY原创
2016-06-06 20:09:291445浏览

前几天,先测试了利用Python脚本登录百度空间,问题已解决,可以参照我的博文《python 脚本登录百度空间》,今天完善了php方式的登录过程,原理同python脚本的登录。 代码如下: 登录代码 private function loginBaiduZone($user,$password){//get Cookie$ret

前几天,先测试了利用Python脚本登录百度空间,问题已解决,可以参照我的博文《python 脚本登录百度空间》,今天完善了php方式的登录过程,原理同python脚本的登录。

代码如下:

登录代码

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请求的工具函数

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

接下来再测试博文的发布代码。

The post PHP 登录百度空间 appeared first on 润物无声.

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn