You are here

function gathercontent_get_command in GatherContent 7

Connecting to the GatherContent API. Data retrieved as JSON.

4 calls to gathercontent_get_command()
GatherContentPages::createNodes in ./gathercontent_pages.inc
Fetch the selected pages from GatherContent, and create nodes.
GatherContentPages::getPages in ./gathercontent_pages.inc
Get pages from GatherContent for a given project, and order them by parent.
gathercontent_api_admin in ./gathercontent_admin.inc
Settings form builder.
gathercontent_import_content_type in ./gathercontent.module
Import wizard step 1: Select content type.

File

./gathercontent.module, line 302
Imports pages from GatherContent (http://gathercontent.com/) into Drupal as nodes.

Code

function gathercontent_get_command($command = '', $postfields = array(), $account_name = '', $api_key = '') {
  if ($account_name == '') {
    $account_name = variable_get('gathercontent_account_name');
  }
  if ($api_key == '') {
    $api_key = variable_get('gathercontent_api_key');
  }
  $api_url = 'https://' . $account_name . '.gathercontent.com/api/0.2/' . $command;
  $postfields = http_build_query($postfields);

  // Using cURL, since GatherContent's API requires Digest authentication,
  // and drupal_http_request() doesn't support that
  // (see https://drupal.org/node/289820).
  $session = curl_init();
  curl_setopt($session, CURLOPT_URL, $api_url);
  curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  curl_setopt($session, CURLOPT_HEADER, FALSE);
  curl_setopt($session, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-Type: application/x-www-form-urlencoded',
  ));
  curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($session, CURLOPT_USERPWD, $api_key . ":x");
  curl_setopt($session, CURLOPT_POST, TRUE);
  curl_setopt($session, CURLOPT_POSTFIELDS, $postfields);
  curl_setopt($session, CURLOPT_SSL_VERIFYPEER, TRUE);
  $response = curl_exec($session);
  $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);
  curl_close($session);
  try {
    $resp = json_decode($response);
    if (isset($resp->success) && $resp->success === TRUE) {
      return $resp;
    }
    elseif (isset($resp->error)) {
      if ($resp->error == 'You have to log in.') {
        $error = 'login_error';
      }
      return gathercontent_error_return($resp->error);
    }
    else {
      return gathercontent_error_return('login_error');
    }
  } catch (Exception $e) {
    return gathercontent_error_return('There was a problem contacting the API. Please check your server allows it.');
  }
  return FALSE;
}