-
Notifications
You must be signed in to change notification settings - Fork 127
Server to Server Communication
For the purposes of this page, I will be referencing Laravel specific configs. However, these steps can be applied to any php application and do not require the use of Laravel.
Also assuming that you have already installed the Infusionsoft PHP-SDK through composer.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateApiTokenTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('api_tokens', function (Blueprint $table) {
$table->increments('id');
$table->string('provider')->unique();
$table->text('token');
$table->text('url')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('api_tokens');
}
}
Migrate your database php artisan migrate
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ApiToken extends Model {
protected $table = 'api_tokens';
public $timestamps = false;
}
In your routes file add
Route::get('/infusionsoft/callback', 'InfusionsoftController@callback');
Note this route path should match the INFUSIONSOFT_REDIRECT_URL
environment variable, otherwise the oauth process will fail.
php artisan make:controller InfusionsoftController
You will want to replace app-name
with the name of your infusionsoft application. This function is what handles storing your api token so that it can be used latter.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ApiToken;
use Infusionsoft\Infusionsoft;
class InfusionsoftController extends Controller {
public function callback (Request $request) {
$apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
if ($apiOBJ != null && $apiOBJ->token) {
Infusionsoft::setToken(unserialize($apiOBJ->token));
}
if ($request->has('code') && !Infusionsoft::getToken()) {
Infusionsoft::requestAccessToken($request->get('code'));
}
if (Infusionsoft::getToken()) {
$apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
if($apiOBJ == null) {
$apiOBJ = new ApiToken;
$apiOBJ->provider = "app-name.infusionsoft.com";
$apiOBJ->token = serialize(Infusionsoft::getToken());
$apiOBJ->save();
} else {
$apiOBJ->token = serialize(Infusionsoft::getToken());
$apiOBJ->save();
}
}
return redirect()->to('/');
}
}
To authorize your new application you will want to visit the url that is generated by Infusionsoft::getAuthorizationUrl()
This will redirect you to Infusionsoft to complete the oauth process.
Now that you have your token you are ready to start making api calls from your server without having to go through the oauth process every time.
$apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
Infusionsoft::setToken(unserialize($apiOBJ->token));
if(Infusionsoft::isTokenExpired()) {
Infusionsoft::refreshAccessToken();
$apiOBJ->token = serialize(Infusionsoft::getToken());
$apiOBJ->save();
}
try {
$tags = Infusionsoft::data()->query('ContactGroupAssign', 1000, 0, ['ContactId' => 123],['DateCreated','GroupId','ContactGroup'], 'DateCreated', false);
} catch (\Infusionsoft\TokenExpiredException $e) {
Infusionsoft::refreshAccessToken();
$apiOBJ->token = serialize(Infusionsoft::getToken());
$apiOBJ->save();
$tags = Infusionsoft::data()->query('ContactGroupAssign', 1000, 0, ['ContactId' => 123],['DateCreated','GroupId','ContactGroup'], 'DateCreated', false);
}