API interface and sample function to determine whether a date is a legal holiday

API interface and sample function
for judging whether a date is a legal holiday It is necessary to determine whether a date is a legal holiday. Since national holidays change every year, it is necessary to use the interface to judge. Unfortunately, there are not many such interfaces, here are three:

http://tool.bitefu.net/jiari/
http://www.easybots.cn/holiday_api.net
https://www.juhe.cn/docs/api/id/177 —— This requires real-name authentication The
first two One looks personal, and the last is a perpetual calendar interface for aggregated data.

Used the first two in my small project. I feel that the personal construction may not be too stable, but I see more and more restrictions on the aggregated api, and only 100 calls can be made per day.

Attached is a piece of code in a small project, which uses curl to obtain the first two interfaces and the caching mechanism of Thinkphp5 to determine whether the current date is a holiday.

function isHoliday()
{
    $today = date('Ymd');

    if (cache($today) !== false) {
        return cache($today);
    } else {
        $api1 = juhecurl('http://tool.bitefu.net/jiari/?d='.$today);
        if (is_numeric($api1)) {
            cache($today, $api1, 86400);
            return cache($today);
        } else {
            $api2 = json_decode(juhecurl('http://www.easybots.cn/api/holiday.php?d='.$today));
            if (is_numeric($api2)) {
                cache($today, $api2->$today, 86400);
                return cache($today);
            } else {
                return -1;
            }
        }
    }
}

The cache() cache helper function in the above code is provided by thinkphp5, and juhecurl() is a curl access function, which is for reference only and cannot be directly transferred to other projects.

Since the return results of the two interfaces are relatively uniform, the working day is 0, the rest day is 1, and the holiday is 2. Therefore, no detailed judgment is made, and -1 is returned if the result is not obtained.

Since the two interfaces are not too standard, it is recommended to check whether they are still available when using them. If you can consider a certain budget, it is recommended to use the aggregation interface stability point.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123667&siteId=291194637