public function HubspotFormsCore::fetchHubspotForms in Hubspot forms 8
Make an API call to Hubspot Forms API and get a list of all available forms.
2 calls to HubspotFormsCore::fetchHubspotForms()
- HubspotFormsCore::getFormIds in src/
HubspotFormsCore.php - Get form ids.
- HubspotFormsCore::isConnected in src/
HubspotFormsCore.php - Check Hubspot connection.
File
- src/
HubspotFormsCore.php, line 48
Class
- HubspotFormsCore
- Class HubspotFormsCore.
Namespace
Drupal\hubspot_formsCode
public function fetchHubspotForms() {
$config = \Drupal::config('hubspot_forms.settings');
$api_key = $config
->get('hubspot_api_key');
try {
// [Get all forms from a portal](http://developers.hubspot.com/docs/methods/forms/v2/get_forms)
$uri = 'https://api.hubapi.com/forms/v2/forms?hapikey=' . $api_key;
$client = \Drupal::httpClient([
'base_url' => $uri,
]);
$request = $client
->request('GET', $uri, [
'timeout' => 5,
'headers' => [
'Accept' => 'application/json',
],
]);
if ($request
->getStatusCode() == 200) {
$response = json_decode($request
->getBody());
if (empty($response)) {
return [];
}
else {
return $response;
}
}
else {
return [];
}
} catch (\GuzzleHttp\Exception\ClientException $e) {
$message = $e
->getMessage() . '. Make sure you provided correct Hubspot API Key on the configuration page.';
\Drupal::logger('hubspot_forms')
->notice($message);
return [];
} catch (\GuzzleHttp\Exception\ConnectException $e) {
$message = $e
->getMessage();
\Drupal::logger('hubspot_forms')
->notice($message);
return [];
}
}