You are here

private function BackupMigrateDropboxAPI::send_http_request in Backup and Migrate Dropbox 7.3

Executes a curl request.

Parameters

string $url:

array $headers:

string $body:

Return value

object An object with the following properties:

  • body: the returned response
  • http_code: the HTTP result code
  • meta: an associative array with meta info about the executed curl request: the result of {@see curl_getinfo()} called without an option passed in) plus an entry 'method_time', containing the time spent in the method (which during local development was at most 0.0002s higher then the key 'total_time' as returned by Curl). Can be used for logging or timing.

Throws

RuntimeException On any error at the curl level, which will be rare, an exception will be thrown. Thus http responses (and codes) that indicate an error are returned as an object.

1 call to BackupMigrateDropboxAPI::send_http_request()
BackupMigrateDropboxAPI::send_message in ./backup_migrate_dropbox.dropbox_api.inc
Sends a request to Dropbox and returns the response.

File

./backup_migrate_dropbox.dropbox_api.inc, line 659

Class

BackupMigrateDropboxAPI
BackupMigrateDropboxAPI contains all the details about the Dropbox api, authorization calls, endpoints, uris, parameters, error handling, and split requests for large uploads/downloads

Code

private function send_http_request($url, $headers, $body) {
  $start = microtime(TRUE);
  $request = $this
    ->get_curl_handle();
  $options = [
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
  ];
  if (!empty($body)) {
    $options[CURLOPT_POSTFIELDS] = $body;
  }
  curl_setopt_array($request, $options);
  $result = curl_exec($request);
  if ($result === FALSE || !empty(curl_error($request))) {
    watchdog('backup_migrate', "Backup Migrate Dropbox Error: send_http_request({$url}): Curl error: " . curl_error($request), [], WATCHDOG_ERROR);
    throw new RuntimeException('Curl error: ' . curl_error($request));
  }
  return (object) [
    'body' => $result,
    'code' => (int) curl_getinfo($request, CURLINFO_RESPONSE_CODE),
    'meta' => curl_getinfo($request) + [
      'method_time' => microtime(TRUE) - $start,
    ],
  ];
}