Scalr_Net_Scalarizr_Client::request PHP Method

request() public method

public request ( $method, stdClass $params = null, $namespace = null )
$params stdClass
    public function request($method, stdClass $params = null, $namespace = null)
    {
        if (!$namespace) {
            $namespace = $this->namespace;
        }
        $requestObj = new stdClass();
        $requestObj->id = microtime(true);
        $requestObj->method = $method;
        $requestObj->params = new stdClass();
        $this->walkSerialize($params, $requestObj->params, 'underScope');
        $this->cryptoTool->setCryptoKey($this->dbServer->GetKey(true));
        $jsonRequest = $this->cryptoTool->encrypt(json_encode($requestObj));
        $dt = new DateTime('now', new DateTimeZone("UTC"));
        $timestamp = $dt->format("D d M Y H:i:s e");
        $signature = $this->cryptoTool->sign($jsonRequest, null, $timestamp, Scalr_Net_Scalarizr_Client::HASH_ALGO);
        $request = new Request("POST");
        // If no VPC router communicating via local inteface (Scalr should be setup within the esame network)
        $requestHost = $this->dbServer->getSzrHost() . ":{$this->port}";
        if ($this->isVPC) {
            $routerFarmRoleId = $this->dbServer->GetFarmRoleObject()->GetSetting(Scalr_Role_Behavior_Router::ROLE_VPC_SCALR_ROUTER_ID);
            if ($routerFarmRoleId) {
                $routerRole = DBFarmRole::LoadByID($routerFarmRoleId);
            } else {
                $routerRole = $this->dbServer->GetFarmObject()->GetFarmRoleByBehavior(ROLE_BEHAVIORS::VPC_ROUTER);
            }
            if ($routerRole) {
                // No public IP need to use proxy
                if (!$this->dbServer->remoteIp) {
                    $requestHost = $routerRole->GetSetting(Scalr_Role_Behavior_Router::ROLE_VPC_IP) . ":80";
                    $request->addHeaders(array("X-Receiver-Host" => $this->dbServer->localIp, "X-Receiver-Port" => $this->port));
                    // There is public IP, can use it
                } else {
                    $requestHost = "{$this->dbServer->remoteIp}:{$this->port}";
                }
            }
        }
        $request->setRequestUrl("http://{$requestHost}/{$namespace}");
        $request->setOptions(array('timeout' => $this->timeout, 'connecttimeout' => 10));
        $request->addHeaders(array("Date" => $timestamp, "X-Signature" => $signature, "X-Server-Id" => $this->dbServer->serverId));
        $request->append($jsonRequest);
        try {
            // Send request
            $response = \Scalr::getContainer()->srzhttp->sendRequest($request);
            $this->debug['responseCode'] = $response->getResponseCode();
            $this->debug['fullResponse'] = $response->toString();
            if ($response->getResponseCode() == 200) {
                $body = $this->cryptoTool->decrypt($response->getBody()->toString());
                $jResponse = @json_decode($body);
                if (!empty($jResponse->error)) {
                    throw new Exception("{$jResponse->error->message} ({$jResponse->error->code}): {$jResponse->error->data}");
                }
                return $jResponse;
            } else {
                throw new Exception(sprintf("Unable to perform request to scalarizr: %s (%s)", $response->getBody()->toString(), $response->getResponseCode()));
            }
        } catch (\http\Exception $e) {
            if (isset($e->innerException)) {
                $msg = $e->innerException->getMessage();
            } else {
                $msg = $e->getMessage();
            }
            if (stristr($msg, "Namespace not found")) {
                $msg = "Feature not supported by installed version of scalarizr. Please update it to the latest version and try again.";
            }
            throw new Exception(sprintf("Unable to perform request to scalarizr: %s", $msg));
        }
    }