How to Retrieve a List of Public Holidays for a Specified Country (in a Specified Year) using PHP
While many companies offer “floating” holidays and other voluntary benefits for their employees, it’s often obligatory to give certain public holidays off each year — and the dates for those holidays can sometimes vary, making them difficult to keep track of.
Thankfully, the free-to-use API solution provided in this article automatically retrieves public holiday information for a specific country in a specific year ,so you won’t have to worry about remembering those details yourself.
All we need to do is post our country code and the current year, and our response will return the name, occurrence date, holiday type, and breadth of observance (i.e., is this holiday observed across the whole country?) of each holiday in its own array.
We can install the client SDK by running the following command:
composer require cloudmersive/cloudmersive_validate_api_client
And we can then structure our API call using the following ready-to-run PHP code examples:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\DateTimeApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\GetPublicHolidaysRequest(); // \Swagger\Client\Model\GetPublicHolidaysRequest | Input request
try {
$result = $apiInstance->dateTimeGetPublicHolidays($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DateTimeApi->dateTimeGetPublicHolidays: ', $e->getMessage(), PHP_EOL;
}
?>
To authenticate our request, we’ll need to provide a free-tier API key in the request body (on the $config line). We can get one by registering a free account on the Cloudmersive website.
No need to worry about public holidays anymore — this API will take care of it for you!