模拟微博登陆,获取微博cookie

以技术角度对待,请勿有违规违法行为;

准备工作:

我们需要安装一个Guzzle package;(当然不装也行);

直接上代码;

        $headers = [
            "User-Agent" => "Mozilla/5.0 (Linux; Android 5.1.1; nxt-al10 Build/LYZ28N) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 sinablog-android/5.3.2 (Android 5.1.1; zh_CN; huawei nxt-al10/nxt-al10)",
            "Content-Type" => "application/x-www-form-urlencoded; charset=utf-8"
        ];

            $client = new \GuzzleHttp\Client();

            //获取微博通行证
            $pass = $this->getWeiboPass($client, $headers);

            if(isset($pass['Set-Cookie'][2])) {
                $cookie = $pass['Set-Cookie'][2];
			}

    public function getWeiboPass($client, $headers){

        $phone = '13203134112';
        $pwd = 'tt823175';

        $time = time();

        $url = "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=" . $time . rand(100,999);

        $data = [
            "form_params"=>[
                "entry" => "sso",
                "gateway" => "1",
                "from" => "null",
                "savestate" => "30",
                "useticket" => "0",
                "pagerefer" => '',
                "vsnf" => "1",
                "su" => base64_encode($phone),
                "service" => "sso",
                "sp" => $pwd,
                "sr" => "1920*1080",
                "encoding" => "UTF-8",
                "cdult" => "3",
                "domain" => "sina.com.cn",
                "prelt" => "0",
                "returntype" => "TEXT",
            ],
            'headers' => $headers

        ];

        $result = $client->post($url, $data);

        return $result->getHeaders();

    }

原理很简单,以base64方式加密账号后发送请求,返回过来的响应头里就有我们需要的cookie;

你可能感兴趣的:(php)