· 9 years ago · Oct 07, 2016, 03:22 PM
1
2 public function getMauticToken(){
3 session_start();
4
5 $publicKey = '';
6 $secretKey = '';
7 $callback = '';
8
9// ApiAuth::initiate will accept an array of OAuth settings
10 $settings = array(
11 'baseUrl' => 'http://sfa.devidia.net', // Base URL of the Mautic instance
12 'version' => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
13 'clientKey' => '1_6dahd61ykjk04cg40sk4g484kgo4cws4ok840cc84sc0gw4ck0', // Client/Consumer key from Mautic
14 'clientSecret' => '3uq1n297op6osw4wg088ckgscs04w4w004g0co8sckgoso0048', // Client/Consumer secret key from Mautic
15 'callback' => 'http://exaleads.daniel/casper/mautic-token' // Redirect URI/Callback URI for this script
16 );
17
18 /*
19 // If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
20 $settings['accessToken'] = $accessToken;
21 $settings['accessTokenSecret'] = $accessTokenSecret; //for OAuth1.0a
22 $settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
23 $settings['refreshToken'] = $refreshToken;
24 */
25 if (!empty($accessTokenData['accessToken']) && !empty($accessTokenData['accessTokenSecret'])) {
26 $settings['accessToken'] = $accessTokenData['accessToken'] ;
27 $settings['accessTokenSecret'] = $accessTokenData['accessTokenSecret'];
28 $settings['accessTokenExpires'] = $accessTokenData['accessTokenExpires'];
29 }
30// Initiate the auth object
31 $auth = new ApiAuth();
32 $auth = $auth->newAuth($settings);
33
34// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or
35// set the access_tokens when the user is redirected back after granting authorization
36
37// If the access token is expired, and a refresh token is set above, then a new access token will be requested
38
39 try {
40 if ($auth->validateAccessToken()) {
41
42 // Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
43 // refresh token
44
45 // $accessTokenData will have the following keys:
46 // For OAuth1.0a: access_token, access_token_secret, expires
47 // For OAuth2: access_token, expires, token_type, refresh_token
48
49 if ($auth->accessTokenUpdated()) {
50 $accessTokenData = $auth->getAccessTokenData();
51 //store access token data however you want
52
53 //test apres recuperation du token
54
55 $api = new MauticApi();
56 $apiUrl = 'http://sfa.devidia.net/api';
57 $contactApi = $api->newApi('contacts', $auth, $apiUrl);
58
59 // Create the contact
60
61 $lead = $contactApi->create(array(
62 'ipAddress' => $_SERVER['REMOTE_ADDR'],
63 'firstname' => 'API',
64 'lastname' => 'TEST2',
65 'origine' => 'VDL',
66 'company' => 'devidia',
67 'email' => 'test@api.daniel',
68 'scraped_from' => 'Test',
69 ));
70 var_dump($lead);
71 }
72 }
73 } catch (Exception $e) {
74 // Do Error handling
75 }
76
77 }