Acquia\CloudApi\Client\CloudApi::callAcapi PHP Method

callAcapi() private method

Call an Acquia Cloud API resource.
private callAcapi ( $site, $method, $resource, $params = [], $body = [], $options = [] )
$site The site name (ex: tangle001)
$method The HTTP method; e.g. GET.
$resource The API function to call; e.g. /sites/:site.
    private function callAcapi($site, $method, $resource, $params = array(), $body = array(), $options = array())
    {
        // Merge in default options.
        $options = $options + $this->default_options;
        // Build the API call URL.
        $endpoint = $this->creds['endpoint'];
        $url = sprintf('%s%s.json', $endpoint, $resource);
        // TODO: Add the caller parameter.
        //    $params['caller'] = acapi_get_option('caller');
        foreach ($params as $k => $v) {
            $params[$k] = "{$k}=" . urlencode($v);
        }
        $url .= '?' . implode('&', $params);
        $creds = $this->getCreds($site);
        // Build the body.
        $json_body = json_encode($body);
        $headers = array();
        $ch = curl_init($url);
        // Basic request settings
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_USERAGENT, basename(__FILE__));
        if (!empty($options['result_stream'])) {
            curl_setopt($ch, CURLOPT_FILE, $options['result_stream']);
        } else {
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        }
        // User authentication
        curl_setopt($ch, CURLOPT_HTTPAUTH, TRUE);
        curl_setopt($ch, CURLOPT_USERPWD, $creds);
        // SSL
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        $verify_peer = empty($options['no_verify_peer']) && preg_match('@^https:@', $endpoint);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_peer);
        if ($this->cainfo) {
            curl_setopt($ch, CURLOPT_CAINFO, $this->cainfo);
        }
        // Redirects
        if (!empty($options['redirect'])) {
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_MAXREDIRS, $options['redirect'] + 1);
        }
        // Body
        if (!empty($body)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_body);
            $headers[] = 'Content-Type: application/json;charset=utf-8';
            $headers[] = 'Content-Length: ' . strlen($json_body);
        }
        // Headers
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if (!empty($options['include_header'])) {
            // Include the header in the output.
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        // Go
        $content = curl_exec($ch);
        if (curl_errno($ch) > 0) {
            throw new CloudApiException(sprintf('ACAPI_CURL_ERROR: Error accessing API: %s (args: %s)', curl_error($ch), print_r(func_get_args, TRUE)));
        }
        $result = json_decode($content);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($status != 200 && $status != 307) {
            switch ($status) {
                case 404:
                    throw new CloudResourceNotFoundException(sprintf('ACAPI_RETURN_CODE: Resource not found. (args: %s)', print_r(func_get_args(), TRUE)), $status);
                    break;
                default:
                    throw new CloudApiException(sprintf('ACAPI_RETURN_CODE: Error returned from Cloud API: %s (result: %s) (args: %s)', $status, print_r($result, TRUE), print_r(func_get_args(), TRUE)), $status);
            }
        }
        if (!empty($options['include_header'])) {
            return array('result' => $result, 'content' => $content);
        }
        return array('result' => $result);
    }