1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace DrewM\MailChimp; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class OAuth |
9
|
|
|
* This class allows one to use the oauth authentication of Mailchimp |
10
|
|
|
* @package DrewM\MailChimp |
11
|
|
|
*/ |
12
|
|
|
class OAuth |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get Mailchimp Authentication url |
17
|
|
|
* |
18
|
|
|
* @param $client_id |
19
|
|
|
* @param $redirect_uri |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function getAuthUrl($client_id, $redirect_uri){ |
23
|
|
|
$encoded_uri = urldecode($redirect_uri); |
24
|
|
|
$authUrl = "https://login.mailchimp.com/oauth2/authorize"; |
25
|
|
|
$authUrl .= "?client_id=" . $client_id; |
26
|
|
|
$authUrl .= "&redirect_uri=" . $encoded_uri; |
27
|
|
|
$authUrl .= "&response_type=code"; |
28
|
|
|
return $authUrl; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get a user access token from the code retrieved with getUrl |
33
|
|
|
* |
34
|
|
|
* @param $code |
35
|
|
|
* @param $client_id |
36
|
|
|
* @param $client_secret |
37
|
|
|
* @param $redirect_uri |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public static function getAccessToken($code, $client_id, $client_secret, $redirect_uri) |
41
|
|
|
{ |
42
|
|
|
$encoded_uri = urldecode($redirect_uri); |
43
|
|
|
$oauth_string = "grant_type=authorization_code"; |
44
|
|
|
$oauth_string .= "&client_id=" . $client_id; |
45
|
|
|
$oauth_string .= "&client_secret=" . $client_secret; |
46
|
|
|
$oauth_string .= "&redirect_uri=" . $encoded_uri; |
47
|
|
|
$oauth_string .= "&code=" . $code; |
48
|
|
|
|
49
|
|
|
return self::exchange($oauth_string); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Internal function that makes call to Mailchimp API to get an access token |
54
|
|
|
* |
55
|
|
|
* @param $oauth_string |
56
|
|
|
* @return string |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
private static function exchange($oauth_string) |
60
|
|
|
{ |
61
|
|
|
$ch = curl_init('https://login.mailchimp.com/oauth2/token'); |
62
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
63
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
64
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $oauth_string); |
65
|
|
|
$return = curl_exec($ch); |
66
|
|
|
if (!is_null(json_decode($return))) { |
67
|
|
|
$return = json_decode($return); |
68
|
|
|
} |
69
|
|
|
curl_close($ch); |
70
|
|
|
if (!$return->access_token) { |
71
|
|
|
throw new \Exception( |
72
|
|
|
'MailChimp did not return an access token', |
73
|
|
|
$return |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
$headers = array('Authorization: OAuth ' . $return->access_token); |
77
|
|
|
$ch = curl_init("https://login.mailchimp.com/oauth2/metadata/"); |
78
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
79
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
80
|
|
|
$account = curl_exec($ch); |
81
|
|
|
if (!is_null(json_decode($account))) { |
82
|
|
|
$account = json_decode($account); |
83
|
|
|
} |
84
|
|
|
curl_close($ch); |
85
|
|
|
if (!$account->dc) { |
86
|
|
|
throw new \Exception( |
87
|
|
|
'Unable to retrieve account meta-data', |
88
|
|
|
$account |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
return $return->access_token . "-" . $account->dc; |
92
|
|
|
} |
93
|
|
|
} |