/** * Login, and get the cookie jar. * * @param void * @return $this or $jar */ 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; } }