Skip to content

Commit 372cadd

Browse files
authored
Merge pull request #89 from infusionsoft/resthooks
Add RESTHook functions
2 parents a2197e6 + 6a0f45f commit 372cadd

File tree

5 files changed

+1133
-947
lines changed

5 files changed

+1133
-947
lines changed

samples/resthookManager.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

samples/verifyRestHook.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
session_start();
4+
5+
require_once '../vendor/autoload.php';
6+
7+
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
8+
'clientId' => '',
9+
'clientSecret' => '',
10+
'redirectUri' => '',
11+
));
12+
13+
// In order to verify the endpoint, we need to return the X-Hook-Secret header.
14+
// By default, the autoverify() function will set the proper header, but if you
15+
// pass false as the first argument to autoverify(false) the function will simply
16+
// return the header value for you to set as you please (handy if you are using
17+
// a PHP class or framework that manages requests for you
18+
19+
$infusionsoft->resthooks()->autoverify();
20+
21+
return;

0 commit comments

Comments
 (0)