PHP Requires Chilkat v11.0.0+
PHP
PayPal: Decide when to renew an access token
See more PayPal Examples
Use the UTC request time and expires_in from the token example. Renew five minutes early. This is a cache decision; a revoked token can still receive HTTP 401 before its calculated expiry.
Chilkat PHP Downloads
<?php
use Chilkat2\DateTime;
use Chilkat2\JsonObject;
// Read the metadata saved by Get a sandbox access token.
$timing = new JsonObject();
$issued = new DateTime();
try {
$timing->LoadFile('paypal-token-timing.json');
} catch (\Chilkat2\Exception $e) {
echo "No timing metadata. Request a token.\n";
exit;
}
try {
$issued->SetFromTimestamp($timing->StringOf('issued_at'));
} catch (\Chilkat2\Exception $e) {
echo "Invalid issue time. Request a token.\n";
exit;
}
$now = new DateTime();
$now->SetFromCurrentSystemTime();
$age = $now->DiffSeconds($issued);
$lifetime = $timing->IntOf('expires_in');
if (($age < 0) || (($lifetime <= 300) || ($age >= ($lifetime - 300)))) {
echo "Request a new client-credentials token.\n";
exit;
}
echo "Reuse the matching token from the credential store.\n";