DEV Community

Discussion on: How to consume RESTful APIs in Laravel 8 and Laravel 7

Collapse
jskogsta profile image
Jorgen Skogstad • Edited on

Got any thoughts as to how that can be extended consuming API's that have to be authenticated with a token similar to github.com/WildApricot/ApiSamples/... ?

Essentially fulfilling the same process as this code?:

$waApiClient = WaApiClient::getInstance();

$waApiClient->initTokenByApiKey('put_your_apikey_here');

$account = getAccountDetails();
$accountUrl = $account['Url'];

$contactsResult = getContactsList(); 
$contacts =  $contactsResult['Contacts'];

foreach($contacts as $contact) {
   echo '<br />';
   echo sprintf("#%d - %s", $contact['Id'], $contact['DisplayName']);
}

function getAccountDetails()
{
   global $waApiClient;
   $url = 'https://api.wildapricot.org/v2/Accounts/';
   $response = $waApiClient->makeRequest($url); 
   return  $response[0]; // usually you have access to one account
}

function getContactsList()
{
   global $waApiClient;
   global $accountUrl;
   $queryParams = array(
      '$async' => 'false', // execute request synchronously
      '$top' => 10, // limit result set to 10 records
      '$filter' => 'Member eq true', // filter only members
      '$select' => 'First name, Last name'
   ); // select only first name and last name to reduce size of json data
   $url = $accountUrl . '/Contacts/?' . http_build_query($queryParams);
   return $waApiClient->makeRequest($url);
}
Enter fullscreen mode Exit fullscreen mode