示例#1
0
 /**
  * Fill the configuration.
  * @param array $config - Configuration.
  */
 public static function config($config)
 {
     Config::load()->fill($config);
 }
 /**
  * Send the request.
  * @return {RawMagisterResponse}
  */
 public function send()
 {
     $startTime = microtime(true);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, 'https://' . Magister::getRMI()->getSchool() . '.magister.net' . $this->url);
     curl_setopt($ch, CURLOPT_USERAGENT, Config::load()->connection_useragent);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_COOKIEJAR, Magister::getRMI()->getSessionFile());
     curl_setopt($ch, CURLOPT_COOKIEFILE, Magister::getRMI()->getSessionFile());
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Config::load()->connection_timeout_connect);
     curl_setopt($ch, CURLOPT_TIMEOUT, Config::load()->connection_timeout_total);
     switch ($this->method) {
         case 'GET':
             break;
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8'));
             break;
         case 'PUT':
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8'));
         case 'DELETE':
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
     }
     /* Wait for a bit to prevent the firewall from hating us. */
     usleep(Config::load()->connection_wait);
     /* Now download. */
     $responseBody = curl_exec($ch);
     if (curl_errno($ch)) {
         throw new MagisterException('cURL Error (' . curl_errno($ch) . '): ' . curl_error($ch));
     }
     curl_close($ch);
     $response = new RawMagisterResponse($this->method, $this->url, $responseBody, $this->body);
     Magister::getRMI()->history[] = (object) array('url' => $this->url, 'method' => $this->method, 'startTime' => $startTime, 'endTime' => microtime(true), 'body' => $this->body, 'response' => $response);
     return $response;
 }