Skip to content

Instantly share code, notes, and snippets.

@brumiser1550
Created November 21, 2017 22:39
Show Gist options
  • Save brumiser1550/39d5151bcb92acfff13b3b147b325b5a to your computer and use it in GitHub Desktop.
Save brumiser1550/39d5151bcb92acfff13b3b147b325b5a to your computer and use it in GitHub Desktop.
How to connect to IFS
<?php
/**
* User: Brandon
* Date: 6/2/2016
* Time: 12:41
*/
require_once __DIR__ . "/config.php";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Connect to Infusionsoft
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Connect to the Infusionsoft API
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => IFS_CLIENT_KEY,
'clientSecret' => IFS_CLIENT_SECRET,
'redirectUri' => IFS_REDIRECT_URL,
));
$storage = new TokenStorage(TOKEN_STORAGE, $infusionsoft);
if (empty($_GET['state'])) {
die("You must come from your account");
}
$app = $_GET['state'];
// Try to get a token from storage
$token = $storage->getToken($app);
// If a token is available in storage, we tell the SDK to use that token for subsequent requests.
if (!empty($token->getAccessToken())) {
$infusionsoft->setToken($token);
}
// If we are returning from Infusionsoft we need to exchange the code for an access token.
// We redirect back to the page to prevent sending the same code to Infusionsoft twice.
if (isset($_GET['code'])) {
$infusionsoft->requestAccessToken($_GET['code']);
$token = $infusionsoft->getToken();
$storage->saveToken($app, $token);
header("Location: " . IFS_REDIRECT_URL . "?state=$app");
die();
}
//Try to refresh if necessary
if ($infusionsoft->getToken() && $infusionsoft->getToken()->endOfLife - time() < 7200) {
$tokenData = $infusionsoft->refreshAccessToken();
$token = $infusionsoft->getToken();
$storage->saveToken($app, $token);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorize Infusionsoft</title>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<?php
//If no token or code lets get one
if (!$infusionsoft->getToken()): ?>
<h2 class="form-signin-heading">Please Authorize Infusionsoft</h2>
<a class="btn btn-lg btn-primary btn-block" href="<?php echo $infusionsoft->getAuthorizationUrl($app); ?>">Authorize</a>
<?php else: ?>
<h2 class="form-signin-heading">Infusionsoft Authorized</h2>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>
<?php
/**
* User: Brandon
* Date: 6/2/2016
* Time: 12:41
*/
require_once __DIR__ . '/config.php';
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Connect to Infusionsoft
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => IFS_CLIENT_KEY,
'clientSecret' => IFS_CLIENT_SECRET,
'redirectUri' => IFS_REDIRECT_URL,
));
$storage = new TokenStorage(TOKEN_STORAGE, $infusionsoft); //write a class to handle your storage
// Try to get a token from storage
$token = $storage->getToken($APP_NAME);
// If a token is available in storage, we tell the SDK to use that token for subsequent requests.
if (!empty($token->getAccessToken())) {
$infusionsoft->setToken($token);
}
//If no token
if (!$infusionsoft->getToken()) {
header("Location: " . IFS_REDIRECT_URL . "?state=$APP_NAME");
die();
}
//Try to refresh if necessary
if ($infusionsoft->getToken()->endOfLife - time() < 7200) {
$tokenData = $infusionsoft->refreshAccessToken();
$token = $infusionsoft->getToken();
$storage->saveToken($APP_NAME, $token);
}
$member_group_tags = [];
$member_group_tags_by_id = [];
$page = 0;
try {
while ($temp_tags = $infusionsoft->data()->query("ContactGroup", 1000, $page, ["GroupCategoryId" => IFS_MEMBER_CATEGORY], ['Id', 'GroupCategoryId', 'GroupDescription', 'GroupName'], 'GroupName', true)) {
if (!empty($temp_tags) && is_array($temp_tags)) {
foreach ($temp_tags as $temp_tag) {
$member_group_tags[$temp_tag['GroupName']] = $temp_tag;
$member_group_tags_by_id[$temp_tag['Id']] = $temp_tag;
}
}
$page++;
}
} catch (Exception $e) {
//TODO: ?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment