Home > php教程 > php手册 > php中curl模拟登陆用户百度知道的例子

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

WBOY
Release: 2016-05-25 16:45:02
Original
1323 people have browsed it

最近弄了一个工具,希望能获取自己百度网盘里面的数据但又不想公开数据,于是想到了模拟登陆百度,用常规的模拟登陆测试了下发现不行,抓取登陆时的数据才发现,其实百度登陆过程中跳转了几次页面,如果仅仅对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即可!

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

代码如下:

<?php
/** 
 * 百度基础类
 * @author  qaulau@hotmail.com
 * @file    baidu.php
 * @date    2013-6-2  www.phprm.com
 */
class baidu {
    private $cookie = &#39;&#39;;
    private $username = &#39;&#39;;
    private $password = &#39;&#39;;
    const COOKIE_DIR = &#39;temp&#39;; //cookie存放目录
    const COOKIE_VALIDATE = 604800; //cookie有效期,默认为7天
    const SECRET_KEY = &#39;hAFS6as8askNBVSuiealkkw&#39;; //密钥用于加密cookie文件名,防止保存的cookie路径被猜测
    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(&#39;/Set-Cookie:((.+)=(.+))$/m &#39;, $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() {
        //生成一个cookie
        $ret = $this->http_request("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true", "", "");
        //获取token并保存cookie
        $ret = $this->http_request("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true", "", "");
        preg_match_all(&#39;/login_token=&#39;( . +) &#39;/&#39;, $ret, $tokens);
        $login_token = $tokens[1][0];
        //登陆并保存cookie
        $post_data = array();
        $post_data[&#39;username&#39;] = $this->username;
        $post_data[&#39;password&#39;] = $this->password;
        $post_data[&#39;token&#39;] = $login_token;
        $post_data[&#39;charset&#39;] = "UTF-8";
        $post_data[&#39;callback&#39;] = "parent.bd12Pass.api.login._postCallback";
        $post_data[&#39;index&#39;] = "0";
        $post_data[&#39;isPhone&#39;] = "false";
        $post_data[&#39;mem_pass&#39;] = "on";
        $post_data[&#39;loginType&#39;] = "1";
        $post_data[&#39;safeflg&#39;] = "0";
        $post_data[&#39;staticpage&#39;] = "https://passport.baidu.com/v2Jump.html";
        $post_data[&#39;tpl&#39;] = "mn";
        $post_data[&#39;u&#39;] = "http://www.baidu.com/";
        $post_data[&#39;verifycode&#39;] = "";
        $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");
        //记录下所有cookie
        $this->writeCookie();
    }
    private function writeCookie() {
        if (!file_exists(self::COOKIE_DIR)) {
            @mkdir(self::COOKIE_DIR) && touch(self::COOKIE_DIR . &#39;/index.html&#39;);
        }
        $filename = self::COOKIE_DIR . &#39;/&#39; . 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 . &#39;/&#39; . md5($this->username . self::SECRET_KEY);
        if ((@filemtime($filename) + self::COOKIE_VALIDATE > time()) && ($cookie = file_get_contents($filename)) != &#39;&#39;) {
            //如果cookie在有效期内且不为空
            $this->cookie = $cookie;
        } else {
            $this->login();
        }
    }
    /** www.phprm.com 
     * 请求页面
     * @param string $url  :页面地址
     * @param string $referef :引用页面
     * @param string $post_data :post数据,如果填写则为post方式否则为get方式
     * 返回页面数据
     */
    public function request($url, $referef = &#39;&#39;, $post_data = &#39;&#39;) {
        return $this->http_request($url, $referef, $post_data, false);
    }
}
?>
Copy after login

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

$baidu = new baidu(&#39;用户名&#39;,&#39;密码&#39;); 
$data = $baidu->request(&#39;http://pan.baidu.com/api/list?channel=chunlei&clienttype=0&web=1&num=100&page=1&dir=%2F&#39;,&#39;http://pan.baidu.com&#39;); 
echo $data;
Copy after login

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


教程地址:

欢迎转载!但请带上文章地址^^

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template