protected function doLogin()
 {
     $builder = new RequestBuilder();
     $builder->setRequestMethod("POST");
     $builder->setEndpoint($this->params['endpoint']);
     $builder->setQuery("/login.php");
     $builder->setQueryParams(array("s" => $this->params['s'], "lang" => "en", "package" => "chatwork", "args" => ""));
     $builder->setAuthentication($this->params['authentication']);
     if (!$this->params['authentication'] instanceof HeadlessAuthentication) {
         throw new \InvalidArgumentException("headless strategy requires headless authentication");
     }
     if (!$this->driver instanceof Driver\CurlDriver) {
         throw new \InvalidArgumentException("headless strategy expects curl driver");
     }
     $request = $builder->build($this, $this->driver);
     $raw_result = $this->driver->request($request);
     $cookies = array();
     foreach ($raw_result[0] as $key => $value) {
         if ('Set-Cookie' == $key) {
             parse_str($value, $tmp);
             $cookies += $tmp;
         }
     }
     $this->cwssid = $cookies['cwssid'];
     if (preg_match("/var\\s+myid\\s+=\\s+'([a-zA-Z0-9]++)'/", $raw_result[1], $match)) {
         $this->myid = $match[1];
     }
     if (preg_match("/var\\s+ACCESS_TOKEN\\s+=\\s+'([a-zA-Z0-9]+)'/", $raw_result[1], $match)) {
         $this->access_token = $match[1];
     }
     if (empty($this->access_token)) {
         throw new Exception("could not logged in to chatwork");
     }
 }
 protected function api($http_method = "GET", $endpoint, $query, $params, $post_field = array())
 {
     if (!$this->initiated) {
         $this->initiate();
     }
     $builder = new RequestBuilder();
     $builder->setRequestMethod($http_method);
     $builder->setEndpoint($endpoint);
     $builder->setQuery($query);
     $builder->setQueryParams($params);
     $builder->setPostField($post_field);
     $builder->setAuthentication($this->params['authentication']);
     $request = $builder->build($this, $this->driver);
     $res = $this->driver->request($request);
     if (strpos($res[0]['HTTP_CODE'], "40") === 0) {
         $response = json_decode($res[1], true);
         throw new UnauthorizedException("errors: " . join(PHP_EOL, $response['errors']));
     } else {
         if (strpos($res[0]['HTTP_CODE'], "50") === 0) {
             $response = json_decode($res[1], true);
             throw new \RuntimeException("errors: " . join(PHP_EOL, $response['errors']));
         }
     }
     return json_decode($res[1], true);
 }