Lndj\Lcrawl::login PHP Method

login() public method

Login, and get the cookie jar.
public login ( )
    public function login()
    {
        //Get the hidden value from login page.
        $response = $this->client->get($this->login_uri);
        $viewstate = $this->parserHiddenValue($response->getBody());
        //The default login post param
        $loginParam = ['viewstate' => '__VIEWSTATE', 'stu_id' => 'TextBox1', 'passwod' => 'TextBox2', 'role' => 'RadioButtonList1', 'button' => 'Button1'];
        if (!empty($this->loginParam)) {
            $loginParam = $this->loginParam;
        }
        $form_params = [$loginParam['viewstate'] => $viewstate, $loginParam['stu_id'] => $this->stu_id, $loginParam['passwod'] => $this->password, $loginParam['role'] => iconv('utf-8', 'gb2312', '学生'), $loginParam['button'] => iconv('utf-8', 'gb2312', '登录')];
        $query = ['form_params' => $form_params];
        //If set to cache cookie
        if ($this->cacheCookie) {
            $jar = new \GuzzleHttp\Cookie\CookieJar();
            $query['cookies'] = $jar;
        }
        //Post to login
        $result = $this->client->request('POST', $this->login_uri, $query);
        //Is logining successful?
        $response = $this->client->get($this->main_page_uri, ['allow_redirects' => false, 'query' => ['xh' => $this->stu_id]]);
        switch ($response->getStatusCode()) {
            case 200:
                return $this->cacheCookie ? $jar : $this;
                break;
            case 302:
                Log::info('The password is wrong!', $query);
                throw new \Exception('The password is wrong!', 1);
                break;
            default:
                Log::error('Maybe the data source is broken!', $response);
                throw new \Exception('Maybe the data source is broken!', 1);
                break;
        }
    }

Usage Example

Beispiel #1
0
 * This is a test file.
 * 
 * @author    Ning Luo <*****@*****.**>
 * @copyright This code is copyright to me.
 * @license   MIT
 * @package   lndj/Lcrawl
 */
require 'vendor/autoload.php';
use Lndj\Lcrawl;
//stu_id
$stu_id = '201404739';
//your password
$password = '******';
$user = ['stu_id' => $stu_id, 'stu_pwd' => $password];
$client = new Lcrawl('http://xuanke.lzjtu.edu.cn/', $user, false);
$client->login();
$all = $client->setUa('Lcrawl Spider V2.0.2')->getGrade();
//setTimeOut()
//setReferer
//set...
dd($all);
// dd($client->login());
// $client->getSchedule();
// $client->getCet();
//Set the login post param
/*
[
    'viewstate' => '__VIEWSTATE',
    'stu_id' => 'TextBox1',
    'passwod' => 'TextBox2',
    'role' => 'RadioButtonList1',