Example #1
0
 /**
  * Returns the response body using the PHP cURL Extension
  * @param Config $config
  * @param string $request_path Request Path/URI
  * @throws HttpException Unable to query server
  */
 public function call(Config $config, $request_path)
 {
     $host = $config->getCloudHost();
     if (strpos(':', $host) !== false) {
         list($host, $port) = explode(':', $host);
     } else {
         $port = '80';
     }
     // Open connection
     $fh = @fsockopen($host, $port, $errno, $error, $this->timeout_ms / 1000);
     if (!$fh) {
         throw new HttpException("Unable to contact server: fsock Error: {$error}", null);
     }
     // Setup HTTP Request headers
     $http_header = "GET {$request_path} HTTP/1.1\r\n";
     $http_header .= "Host: {$host}\r\n";
     if ($this->use_compression === true) {
         $http_header .= "Accept-Encoding: gzip\r\n";
     }
     $http_header .= "Accept: */*\r\n";
     $http_header .= "Authorization: Basic " . base64_encode($config->api_key) . "\r\n";
     foreach ($this->request_headers as $key => $value) {
         $http_header .= "{$key}: {$value}\r\n";
     }
     $http_header .= "Connection: Close\r\n";
     $http_header .= "\r\n";
     //die('<pre>'.nl2br($http_header).'</pre>');
     // Setup timeout
     stream_set_timeout($fh, 0, $this->timeout_ms * 1000);
     // Send Request headers
     fwrite($fh, $http_header);
     // Get Response
     $response = '';
     while ($line = fgets($fh)) {
         $response .= $line;
     }
     $stream_info = stream_get_meta_data($fh);
     fclose($fh);
     // Check for Timeout
     if ($stream_info['timed_out']) {
         throw new HttpException("HTTP Request timed out", null);
     }
     $this->processResponse($response);
 }
Example #2
0
 /**
  * Returns the response body using the PHP cURL Extension
  * @param Config $config
  * @param string $request_path Request Path/URI
  * @throws HttpException Unable to query server
  */
 public function call(Config $config, $request_path)
 {
     // Setup
     $this->guzzle->setBaseUrl('http://' . $config->getCloudHost());
     $options = array('auth' => explode(':', $config->api_key, 2), 'timeout' => $this->timeout_ms / 1000, 'connect_timeout' => $this->timeout_ms / 1000, 'proxy' => $this->proxy);
     // Compression
     if ($this->use_compression === true) {
         $this->request_headers['Accept-Encoding'] = 'gzip';
     }
     // Execute
     try {
         $request = $this->guzzle->get($request_path, $this->request_headers, $options);
         $response = $request->send();
     } catch (BadResponseException $e) {
         return $this->processGuzzleResponse($e->getResponse());
     } catch (\Exception $e) {
         throw new HttpException("Unable to contact server: Guzzle Error: " . $e->getMessage(), null, $e);
     }
     return $this->processGuzzleResponse($response);
 }
Example #3
0
 /**
  * Returns the response body using the PHP cURL Extension
  * @param Config $config
  * @param string $request_path Request Path/URI
  * @throws HttpException Unable to query server
  */
 public function call(Config $config, $request_path)
 {
     // Setup CURL
     $this->option(CURLOPT_URL, 'http://' . $config->getCloudHost() . $request_path);
     $this->option(CURLOPT_RETURNTRANSFER, true);
     $this->option(CURLOPT_FORBID_REUSE, true);
     $this->option(CURLOPT_HEADER, true);
     $this->option(CURLOPT_HTTPHEADER, $this->getCurlHeaders());
     $this->option(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     $this->option(CURLOPT_USERPWD, $config->api_key);
     // Proxy
     if ($this->proxy) {
         $this->option(CURLOPT_PROXY, $this->proxy);
     }
     // Compression
     if ($this->use_compression === true && $this->curl_supports_encoding === true) {
         $this->option(CURLOPT_ENCODING, '');
     }
     // Timeout
     if ($this->curl_supports_ms) {
         // Required for CURLOPT_TIMEOUT_MS to play nice on most Unix/Linux systems
         // http://www.php.net/manual/en/function.curl-setopt.php#104597
         $this->option(CURLOPT_NOSIGNAL, 1);
         $this->option(CURLOPT_TIMEOUT_MS, $this->timeout_ms);
     } else {
         $timeout = $this->timeout_ms < 1000 ? 1000 : $this->timeout_ms;
         $this->option(CURLOPT_TIMEOUT, $timeout / 1000);
     }
     // Execute
     $response = curl_exec($this->curl_handle);
     $curl_errno = curl_errno($this->curl_handle);
     $curl_error = curl_error($this->curl_handle);
     $curl_info = curl_getinfo($this->curl_handle);
     if ($curl_errno !== 0) {
         throw new HttpException("Unable to contact server: cURL Error: {$curl_error}", null);
     }
     $this->processResponse($response);
 }