You are here

function hubspot_get_recent in HubSpot 6.2

Same name and namespace in other branches
  1. 6 hubspot.module \hubspot_get_recent()
  2. 7.3 hubspot.module \hubspot_get_recent()
  3. 7 hubspot.module \hubspot_get_recent()
  4. 7.2 hubspot.module \hubspot_get_recent()

Gets the most recent HubSpot leads

Docs: http://docs.hubapi.com/wiki/Searching_Leads

Parameters

int $n Number of leads to fetch:

1 call to hubspot_get_recent()
hubspot_block in ./hubspot.module
Implements hook_block() to provide a HubSpot recent leads block.

File

./hubspot.module, line 393
Sends Webform results to HubSpot's Leads API by using Webform's provided hooks.

Code

function hubspot_get_recent($n = 5) {
  $access_token = variable_get('hubspot_access_token', '');
  $n = intval($n);
  if (empty($access_token)) {
    return array(
      'Error' => t('This site is not connected to a Hubspot Account.'),
    );
  }
  $r = drupal_http_request("https://api.hubapi.com/leads/v1/list/?access_token={$access_token}&sort=insertedAt&dir=desc&max={$n}");
  if ($r['HTTPCode'] == '401') {
    $refresh = hubspot_oauth_refresh();
    if ($refresh) {
      $access_token = variable_get('hubspot_access_token', '');
      $r = drupal_http_request("https://api.hubapi.com/leads/v1/list/?access_token={$access_token}&sort=insertedAt&dir=desc&max={$n}");
    }
  }
  return array(
    'Data' => json_decode($r->data),
    'Error' => isset($r->error) ? $r->error : '',
    'HTTPCode' => $r->code,
  );
}