Example #1
0
 /**
  * @inherit
  */
 public function makeRequest($method, $path, $data = array())
 {
     $url = $this->getEndpoint() . $path;
     $context_options = array($this->scheme => array('method' => 'GET', 'ignore_errors' => true, 'header' => "Content-type: application/json\r\n" . "Accept: application/json;stream=true\r\n" . "User-Agent: " . Version::userAgent() . "\r\n" . "X-Stream: true\r\n"));
     if ($this->username && $this->password) {
         $encodedAuth = base64_encode($this->username . ':' . $this->password);
         $context_options[$this->scheme]['header'] .= 'Authorization: Basic ' . $encodedAuth . "\r\n";
     }
     switch ($method) {
         case self::DELETE:
             $context_options[$this->scheme]['method'] = self::DELETE;
             break;
         case self::POST:
         case self::PUT:
             $dataString = $this->encodeData($data);
             $context_options[$this->scheme]['method'] = $method;
             $context_options[$this->scheme]['content'] = $dataString;
             $context_options[$this->scheme]['header'] .= 'Context-Length: ' . strlen($dataString) . "\r\n";
             break;
     }
     $context = stream_context_create($context_options);
     $response = file_get_contents($url, false, $context);
     if ($response === false) {
         throw new Exception("Can't open connection to " . $url);
     }
     // $http_response_header is set by file_get_contents with the http:// wrapper
     preg_match('/^HTTP\\/1\\.[0-1] (\\d{3})/', $http_response_header[0], $parts);
     $code = $parts[1];
     if (!$code) {
         $code = 500;
         $response = json_encode(array("error" => 'error [' . $code . ']'));
     }
     $bodyData = json_decode($response, true);
     $headers = array();
     foreach ($http_response_header as $header) {
         $parts = explode(':', $header, 2);
         if (count($parts) == 2) {
             $headers[$parts[0]] = $parts[1];
         }
     }
     return array('code' => $code, 'headers' => $headers, 'data' => $bodyData);
 }
Example #2
0
 /**
  * @inherit
  */
 public function makeRequest($method, $path, $data = array())
 {
     //print_r($path);
     //print_r($data);
     $url = $this->getEndpoint() . $path;
     //echo $url."\n ";
     $options = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array('Accept: application/json;stream=true', 'Content-type: application/json', 'User-Agent: ' . Version::userAgent(), 'X-Stream: true'), CURLOPT_CUSTOMREQUEST => self::GET, CURLOPT_POST => false, CURLOPT_POSTFIELDS => null, CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4);
     if ($this->username && $this->password) {
         $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
         $options[CURLOPT_USERPWD] = $this->username . ':' . $this->password;
     }
     switch ($method) {
         case self::DELETE:
             $options[CURLOPT_CUSTOMREQUEST] = self::DELETE;
             break;
         case self::POST:
         case self::PUT:
             $dataString = $this->encodeData($data);
             $options[CURLOPT_CUSTOMREQUEST] = $method;
             $options[CURLOPT_POSTFIELDS] = $dataString;
             $options[CURLOPT_HTTPHEADER][] = 'Content-Length: ' . strlen($dataString);
             $options[CURLOPT_HTTPHEADER][] = 'Accept-Encoding: gzip,deflate';
             if (self::POST == $method) {
                 $options[CURLOPT_POST] = true;
             }
             break;
     }
     $ch = $this->getHandle();
     curl_setopt_array($ch, $options);
     $response = curl_exec($ch);
     //print_r($response);
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
     if ($response === false) {
         throw new Exception("Can't open connection to " . $url);
     }
     if ($code === 401) {
         throw new Exception("database '" . trim($this->getDatabaseName(), "/") . "' does not exist. Please check database configuration. ");
     } else {
         if ($code === 500) {
             $bodyString = substr($response, $headerSize);
             $result_array = json_decode($bodyString, true);
             throw new Exception($result_array['errors'][0]['content'], $result_array['errors'][0]['code']);
         }
     }
     if (!$code) {
         $code = 500;
         $headerSize = 0;
         $response = json_encode(array("error" => curl_error($ch) . ' [' . curl_errno($ch) . ']'));
     }
     $bodyString = substr($response, $headerSize);
     $bodyData = json_decode($bodyString, true);
     $headerString = substr($response, 0, $headerSize);
     $headers = explode("\r\n", $headerString);
     foreach ($headers as $i => $header) {
         unset($headers[$i]);
         $parts = explode(':', $header);
         if (isset($parts[1])) {
             $name = trim(array_shift($parts));
             $value = join(':', $parts);
             $headers[$name] = $value;
         }
     }
     return array('code' => $code, 'headers' => $headers, 'data' => $bodyData);
 }