|
| 1 | +<?php |
| 2 | + |
| 3 | +session_start(); |
| 4 | +//session_destroy(); |
| 5 | + |
| 6 | +require_once '../vendor/autoload.php'; |
| 7 | + |
| 8 | +$infusionsoft = new \Infusionsoft\Infusionsoft(array( |
| 9 | + 'clientId' => '', |
| 10 | + 'clientSecret' => '', |
| 11 | + 'redirectUri' => '', |
| 12 | +)); |
| 13 | + |
| 14 | +// By default, the SDK uses the Guzzle HTTP library for requests. To use CURL, |
| 15 | +// you can change the HTTP client by using following line: |
| 16 | +// $infusionsoft->setHttpClient(new \Infusionsoft\Http\CurlClient()); |
| 17 | + |
| 18 | +// If the serialized token is available in the session storage, we tell the SDK |
| 19 | +// to use that token for subsequent requests. |
| 20 | +if (isset($_SESSION['token'])) { |
| 21 | + $infusionsoft->setToken(unserialize($_SESSION['token'])); |
| 22 | +} |
| 23 | + |
| 24 | +// If we are returning from Infusionsoft we need to exchange the code for an |
| 25 | +// access token. |
| 26 | +if (isset($_GET['code']) and !$infusionsoft->getToken()) { |
| 27 | + $infusionsoft->requestAccessToken($_GET['code']); |
| 28 | + $_SESSION['token'] = serialize($infusionsoft->getToken()); |
| 29 | +} |
| 30 | + |
| 31 | +function resthookManager($infusionsoft) { |
| 32 | + $resthooks = $infusionsoft->resthooks(); |
| 33 | + |
| 34 | + // first, create a new task |
| 35 | + $resthook = $resthooks->create([ |
| 36 | + 'eventKey' => 'contact.add', |
| 37 | + 'hookUrl' => 'http://infusionsoft.app/verifyRestHook.php' |
| 38 | + ]); |
| 39 | + var_dump($resthook); |
| 40 | + $resthook = $resthooks->find($resthook->id)->verify(); |
| 41 | + |
| 42 | + return $resthook; |
| 43 | +} |
| 44 | + |
| 45 | +if ($infusionsoft->getToken()) { |
| 46 | + try { |
| 47 | + $resthook = resthookManager($infusionsoft); |
| 48 | + } |
| 49 | + catch (\Infusionsoft\TokenExpiredException $e) { |
| 50 | + // If the request fails due to an expired access token, we can refresh |
| 51 | + // the token and then do the request again. |
| 52 | + $infusionsoft->refreshAccessToken(); |
| 53 | + |
| 54 | + // Save the serialized token to the current session for subsequent requests |
| 55 | + $_SESSION['token'] = serialize($infusionsoft->getToken()); |
| 56 | + |
| 57 | + $resthook = resthookManager($infusionsoft); |
| 58 | + } |
| 59 | + |
| 60 | + var_dump($resthook); |
| 61 | +} |
| 62 | +else { |
| 63 | + echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to authorize</a>'; |
| 64 | +} |
0 commit comments