/** * Makes api request * * @param string $qid The id of the query. * @param array $options optional Query options for the request. * @param string $path optional Uri path for the request (/user by default) * @param string $method optional Http method (GET by default) * @return object Returns object that is an response data. * @throws CloudynException */ public function call($qid, array $options = array(), $path = '/user', $method = 'GET') { $options['qid'] = (string) $qid; $options['out'] = self::OUT_JSON; if (!isset($options['rqid'])) { $options['rqid'] = $this->getRequestId(); } if (!isset($options['apiversion'])) { $options['apiversion'] = '0.4'; } $this->request = $this->createNewRequest(); $this->request->setUrl($this->getUrl() . $path); $this->request->setMethod(constant('HTTP_METH_' . strtoupper($method))); $this->request->setOptions(array('redirect' => 10, 'useragent' => 'Scalr Client (http://scalr.com)')); $this->request->addQueryData($options); //This line is very necessary or HttpResponce will add stored cookies $this->request->resetCookies(); $this->message = $this->tryCall($this->request); $json = $this->message->getBody(); $json = preg_replace('#^[^\\{\\[]+|[^\\}\\]]+$#', '', trim($json)); $obj = json_decode($json); if (isset($obj->status) && $obj->status != 'ok' && isset($obj->message)) { throw new CloudynException('Cloudyn error. ' . $obj->message); } return $obj; }
protected function request($uri, $method, $data) { $httpRequest = new HttpRequest(); $httpRequest->setOptions(array("useragent" => "Scalr (https://scalr.net)")); $httpRequest->setUrl("{$this->apiUrl}{$uri}"); $httpRequest->setMethod($method); $httpRequest->resetCookies(); $httpRequest->addHeaders(array('Cookie' => $this->sessionCookie, 'Content-Type' => 'application/nimbula-v1+json')); switch ($method) { case HTTP_METH_POST: $httpRequest->setRawPostData(json_encode($data)); $httpRequest->addHeaders(array('Content-Type' => 'application/nimbula-v1+json')); break; } try { $httpRequest->send(); $data = $httpRequest->getResponseData(); $result = @json_decode($data['body']); if ($httpRequest->getResponseCode() > 204) { $message = $result->message; if ($message) { if ($message instanceof stdClass) { $r = (array) $message; $msg = ''; foreach ($r as $k => $v) { $msg .= "{$k}: {$v} "; } throw new Exception(trim($msg)); } else { throw new Exception($message); } } throw new Exception($data['body']); } $headers = $httpRequest->getResponseHeader('Set-Cookie'); if ($headers) { if (!is_array($headers)) { if (stristr($headers, "nimbula")) { $this->sessionCookie = $headers; } } else { } } $this->LastResponseHeaders = $data['headers']; return $result; } catch (Exception $e) { if ($e->innerException) { $message = $e->innerException->getMessage(); } else { $message = $e->getMessage(); } throw new Exception("Nimbula error: {$message}"); } }
/** * Gets configured HttpRequest * * @return \HttpRequest */ private function getRequest() { if (!$this->request) { $opt = ['timeout' => 15]; if (\Scalr::config('scalr.aws.use_proxy')) { $proxy = \Scalr::config('scalr.connections.proxy'); if (in_array($proxy['use_on'], array('both', 'scalr'))) { $opt['proxyhost'] = $proxy['host']; $opt['proxyport'] = $proxy['port']; $opt['proxytype'] = $proxy['type']; if (!empty($proxy['pass']) && !empty($proxy['user'])) { $opt['proxyauth'] = "{$proxy['user']}:{$proxy['pass']}"; $opt['proxyauthtype'] = $proxy['authtype']; } } } $this->request = new \HttpRequest(null, HTTP_METH_GET, $opt); } else { $this->request->resetCookies(true); $this->request->clearHistory(); } return $this->request; }
public function getContent($params = array()) { /* @var $endpoint Endpoint */ $endpoint = $this->getEnvironment()->getPlatformConfigValue(Ec2PlatformModule::ACCOUNT_TYPE) == Ec2PlatformModule::ACCOUNT_TYPE_GOV_CLOUD ? new GovEndpoint() : new Endpoint(); $data = []; $services = ['Amazon Elastic Compute Cloud', 'Amazon Relational Database Service', 'Amazon Simple Storage Service']; $awsCachePath = CACHEPATH . "/{$endpoint->cacheFile}"; if (empty($params['locations'])) { $neededLocations = $this->getUsedLocations(); $params['locations'] = $neededLocations; } else { $neededLocations = $params['locations']; } if (file_exists($awsCachePath) && time() - filemtime($awsCachePath) < 3600) { clearstatcache(); $time = filemtime($awsCachePath); $data = (array) json_decode(file_get_contents($awsCachePath)); } else { $req = new HttpRequest(); $req->resetCookies(); $req->setOptions(array('redirect' => 10, 'useragent' => "Scalr " . SCALR_VERSION, 'verifypeer' => false, 'verifyhost' => false, 'timeout' => 30, 'connecttimeout' => 30)); if (\Scalr::config('scalr.aws.use_proxy') && in_array(\Scalr::config('scalr.connections.proxy.use_on'), array('both', 'scalr'))) { $proxySettings = \Scalr::config('scalr.connections.proxy'); } if (!empty($proxySettings)) { $req->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]); if ($proxySettings['user']) { $req->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]); } } $req->setMethod(constant('HTTP_METH_GET')); $req->setUrl($endpoint->statUrl); try { $req->send(); if ($req->getResponseCode() == 200) { $html = $req->getResponseBody(); } else { return []; } } catch (\HttpException $e) { return []; } //$html = @file_get_contents($endpoint->statUrl); if ($html) { $dom = new DOMDocument(); $dom->validateOnParse = false; @$dom->loadHTML($html); $dom->preserveWhiteSpace = false; foreach ($endpoint->compliance as $compKey => $compValue) { $div = $dom->getElementById($compValue['name']); $tables = $div->getElementsByTagName('table'); $rows = $tables->item(0)->getElementsByTagName('tr'); foreach ($rows as $row) { $cols = $row->getElementsByTagName('td'); if ($cols->length == 0) { continue; } if (preg_match('/(.*)(' . implode('|', $services) . ')(.*)/', $cols->item(1)->nodeValue)) { $regionFilter = $compValue['filter']; if (is_array($compValue['filter'])) { $regionFilter = implode('|', $compValue['filter']); } if (preg_match('/(.*)(' . $regionFilter . ')(.*)/', $cols->item(1)->nodeValue)) { $img = ''; $message = ''; if ($cols->item(0)->getElementsByTagName('img')->item(0)->getAttribute('src') == '/images/status0.gif') { $img = 'normal.png'; } else { $img = 'disruption.png'; $message = $cols->item(2)->nodeValue; } $data[$compKey][substr(str_replace($services, array('EC2', 'RDS', 'S3'), $cols->item(1)->nodeValue), 0, strpos(str_replace($services, array('EC2', 'RDS', 'S3'), $cols->item(1)->nodeValue), ' ('))] = array('img' => $img, 'status' => $cols->item(2)->nodeValue, 'message' => $message); $data[$compKey]['locations'] = $compKey; } } } } file_put_contents($awsCachePath, json_encode($data)); } else { return []; } } $retval = array('locations' => json_encode($neededLocations)); foreach ($neededLocations as $value) { $retval['data'][] = $data[$value]; } return $retval; }