if ($infusionsoft->getToken()) { // Save the serialized token to the current session for subsequent requests // NOTE: this can be saved in your database - make sure to serialize the // entire token for easy future access Session::put('token', serialize($infusionsoft->getToken())); // Now redirect the user to a page that performs some Infusionsoft actions return redirect()->to('/contacts'); } // something didn't work, so let's go back to the beginning return redirect()->to('/'); }); $app->get('/contacts', function () use($app) { // Setup a new Infusionsoft SDK object $infusionsoft = new \Infusionsoft\Infusionsoft(array('clientId' => getenv('INFUSIONSOFT_CLIENT_ID'), 'clientSecret' => getenv('INFUSIONSOFT_CLIENT_SECRET'), 'redirectUri' => getenv('INFUSIONSOFT_REDIRECT_URL'))); // Set the token if we have it in storage (in this case, a session) $infusionsoft->setToken(unserialize(Session::get('token'))); try { // Retrieve a list of contacts by querying the data service $contacts = $infusionsoft->data->query('Contact', 10, 0, ['FirstName' => 'John'], ['FirstName', 'LastName', 'Email', 'ID'], 'FirstName', true); } catch (\Infusionsoft\TokenExpiredException $e) { // Refresh our access token since we've thrown a token expired exception $infusionsoft->refreshAccessToken(); // We also have to save the new token, since it's now been refreshed. // We serialize the token to ensure the entire PHP object is saved // and not accidentally converted to a string Session::put('token', serialize($infusionsoft->getToken())); // Retrieve the list of contacts again now that we have a new token $contacts = $infusionsoft->data->query('Contact', 10, 0, ['FirstName' => 'John'], ['FirstName', 'LastName', 'Email', 'ID'], 'FirstName', true); } return $contacts; });