Пример #1
0
 protected function request($method, $url, $body = null, $raw = false)
 {
     $auth = base64_encode(sprintf('%s:%s', $this->user, $this->pass));
     $headers = array('Host: ' . $this->getPeerIdentity(), 'Authorization: Basic ' . $auth, 'Connection: close');
     if (!$raw) {
         $headers[] = 'Accept: application/json';
     }
     if ($body !== null) {
         $body = json_encode($body);
         $headers[] = 'Content-Type: application/json';
     }
     $opts = array('http' => array('protocol_version' => '1.1', 'user_agent' => 'Icinga Web 2.0 - Director', 'method' => strtoupper($method), 'content' => $body, 'header' => $headers, 'ignore_errors' => true), 'ssl' => array('verify_peer' => false));
     $context = stream_context_create($opts);
     Benchmark::measure('Rest Api, sending ' . $url);
     $res = file_get_contents($this->url($url), false, $context);
     if (substr(array_shift($http_response_header), 0, 10) !== 'HTTP/1.1 2') {
         throw new Exception($res);
     }
     Benchmark::measure('Rest Api, got response');
     if ($raw) {
         return $res;
     } else {
         return RestApiResponse::fromJsonResult($res);
     }
 }
 protected function curlRequest($method, $url, $body = null, $raw = false)
 {
     $auth = sprintf('%s:%s', $this->user, $this->pass);
     $headers = array('Host: ' . $this->getPeerIdentity(), 'Connection: close');
     if (!$raw) {
         $headers[] = 'Accept: application/json';
     }
     if ($body !== null) {
         $body = json_encode($body);
         $headers[] = 'Content-Type: application/json';
     }
     $curl = $this->curl();
     $opts = array(CURLOPT_URL => $this->url($url), CURLOPT_HTTPHEADER => $headers, CURLOPT_USERPWD => $auth, CURLOPT_CUSTOMREQUEST => strtoupper($method), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false);
     if ($body !== null) {
         $opts[CURLOPT_POSTFIELDS] = $body;
     }
     curl_setopt_array($curl, $opts);
     Benchmark::measure('Rest Api, sending ' . $url);
     $res = curl_exec($curl);
     if ($res === false) {
         throw new Exception('CURL ERROR: ' . curl_error($curl));
     }
     Benchmark::measure('Rest Api, got response');
     if ($raw) {
         return $res;
     } else {
         return RestApiResponse::fromJsonResult($res);
     }
 }