Expand on my Instagram Basic Display API hook, using PHP, to check if token is close to expiring and fetch/store new token so it never 'expires'.
I am using Processwire's API but the set up is fairly standard.
// Instagram init
$instagramHttp = new WireHttp();
$instagramAccessToken = "???";
$instagramFields = "username,id,media_type,media_url,caption,tags,timestamp";
$instagramBase = "[login to view URL]$instagramFields&access_token=$instagramAccessToken";
// [login to view URL]$instagramAccessToken
// Instagram feed
wire()->addHookMethod('Page::instagramFeed', function($event) use($instagramHttp, $instagramBase) {
$page = $event->object;
// Get cache only if it's less than or equal to 30 minutes (1800 seconds)
$result = wire('cache')->get('instagramFeed', 1800);
if (!$result) {
$result = $instagramHttp->getJSON($instagramBase);
wire('cache')->save('instagramFeed', $result, 1800); // 30 minutes
}
$event->return = $result;
});
Deliverables
Block of code that I can use to fetch an Instagram feed and store the access token and check if the token has expired.