Skip to content

Commit cfd050c

Browse files
authored
Merge pull request #105 from Kit/v4-api-key-support
Add v4 API Key Support
2 parents bc1b033 + 991b431 commit cfd050c

9 files changed

+1304
-480
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
CONVERTKIT_OAUTH_REFRESH_TOKEN=${{ secrets.CONVERTKIT_OAUTH_REFRESH_TOKEN }}
5252
CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA=${{ secrets.CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA }}
5353
CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA=${{ secrets.CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA }}
54+
CONVERTKIT_API_KEY=${{ secrets.CONVERTKIT_API_KEY }}
5455
CONVERTKIT_OAUTH_CLIENT_ID=${{ secrets.CONVERTKIT_OAUTH_CLIENT_ID }}
5556
CONVERTKIT_OAUTH_CLIENT_SECRET=${{ secrets.CONVERTKIT_OAUTH_CLIENT_SECRET }}
5657
CONVERTKIT_OAUTH_REDIRECT_URI=${{ secrets.CONVERTKIT_OAUTH_REDIRECT_URI }}

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# ConvertKit SDK PHP
1+
# Kit SDK PHP
22

3-
The ConvertKit PHP SDK provides convinient access to the ConvertKit API from applications written in the PHP language.
3+
The Kit PHP SDK provides convinient access to the Kit API from applications written in the PHP language.
44

55
It includes a pre-defined set of methods for interacting with the API.
66

@@ -10,6 +10,7 @@ It includes a pre-defined set of methods for interacting with the API.
1010
|-------------|-------------|--------------------|--------------|
1111
| 1.x | v3 | API Key and Secret | 7.4+ |
1212
| 2.x | v4 | OAuth | 8.0+ |
13+
| 2.2+ | v4 | API Key | 8.0+ |
1314

1415
Refer to [this guide](MIGRATION.md) for changes when upgrading to the v2 SDK.
1516

@@ -41,9 +42,9 @@ If you use Composer, these dependencies should be handled automatically.
4142

4243
### 2.x (v4 API, OAuth, PHP 8.0+)
4344

44-
First, register your OAuth application in the `OAuth Applications` section at https://app.convertkit.com/account_settings/advanced_settings.
45+
First, register your OAuth application in the `OAuth Applications` section at https://app.kit.com/account_settings/advanced_settings.
4546

46-
Using the supplied Client ID and secret, redirect the user to ConvertKit to grant your application access to their ConvertKit account.
47+
Using the supplied Client ID and secret, redirect the user to Kit to grant your application access to their Kit account.
4748

4849
```php
4950
// Require the autoloader (if you're using a PHP framework, this may already be done for you).
@@ -59,7 +60,7 @@ $api = new \ConvertKit_API\ConvertKit_API(
5960
header('Location: '.$api->get_oauth_url('<your_redirect_uri>'));
6061
```
6162

62-
Once the user grants your application access to their ConvertKit account, they'll be redirected to your Redirect URI with an authorization code. For example:
63+
Once the user grants your application access to their Kit account, they'll be redirected to your Redirect URI with an authorization code. For example:
6364

6465
`your-redirect-uri?code=<auth_code>`
6566

@@ -118,26 +119,40 @@ $api = new \ConvertKit_API\ConvertKit_API(
118119
API requests may then be performed:
119120

120121
```php
121-
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com');
122+
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');
122123
```
123124

124125
To determine whether a new entity / relationship was created, or an existing entity / relationship updated, inspect the HTTP code of the last request:
125126

126127
```php
127-
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com');
128+
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');
128129
$code = $api->getResponseInterface()->getStatusCode(); // 200 OK if e.g. a subscriber already added to the specified form, 201 Created if the subscriber added to the specified form for the first time.
129130
```
130131

131132
The PSR-7 response can be fetched and further inspected, if required - for example, to check if a header exists:
132133

133134
```php
134-
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com');
135+
$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');
135136
$api->getResponseInterface()->hasHeader('Content-Length'); // Check if the last API request included a `Content-Length` header
136137
```
137138

139+
### 2.2+ (v4 API, API Key, PHP 8.0+)
140+
141+
Get your Kit API Key and API Secret [here](https://app.kit.com/account_settings/developer_settings) and set it somewhere in your application.
142+
143+
```php
144+
// Require the autoloader (if you're using a PHP framework, this may already be done for you).
145+
require_once 'vendor/autoload.php';
146+
147+
// Initialize the API class.
148+
$api = new \ConvertKit_API\ConvertKit_API(
149+
apiKey: '<your_v4_api_key>'
150+
);
151+
```
152+
138153
### 1.x (v3 API, API Key and Secret, PHP 7.4+)
139154

140-
Get your ConvertKit API Key and API Secret [here](https://app.convertkit.com/account/edit) and set it somewhere in your application.
155+
Get your Kit API Key and API Secret [here](https://app.kit.com/account_settings/developer_settings) and set it somewhere in your application.
141156

142157
```php
143158
// Require the autoloader (if you're using a PHP framework, this may already be done for you).
@@ -149,7 +164,7 @@ $api = new \ConvertKit_API\ConvertKit_API('<your_public_api_key>', '<your_secret
149164

150165
## Handling Errors
151166

152-
The ConvertKit PHP SDK uses Guzzle for all HTTP API requests. Errors will be thrown as Guzzle's `ClientException` (for 4xx errors),
167+
The Kit PHP SDK uses Guzzle for all HTTP API requests. Errors will be thrown as Guzzle's `ClientException` (for 4xx errors),
153168
or `ServerException` (for 5xx errors).
154169

155170
```php
@@ -190,4 +205,4 @@ See the [PHP SDK docs](./docs/classes/ConvertKit_API/ConvertKit_API.md)
190205

191206
See our [contributor guide](CONTRIBUTING.md) for setting up your development environment, testing and submitting a PR.
192207

193-
For ConvertKit, refer to the [deployment guide](DEPLOYMENT.md) on how to publish a new release.
208+
For Kit, refer to the [deployment guide](DEPLOYMENT.md) on how to publish a new release.

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"ConvertKit_API\\": "src/"
3131
}
3232
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"": "tests/"
36+
}
37+
},
3338
"minimum-stability": "dev",
3439
"prefer-stable": true,
3540
"scripts": {

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<testsuites>
44
<testsuite name="ConvertKit API Tests">
55
<directory>tests</directory>
6+
<exclude>tests/ConvertKitAPITest.php</exclude>
67
</testsuite>
78
</testsuites>
89
</phpunit>

src/ConvertKit_API.php

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,22 @@ class ConvertKit_API
6262
* @param string $clientID OAuth Client ID.
6363
* @param string $clientSecret OAuth Client Secret.
6464
* @param string $accessToken OAuth Access Token.
65+
* @param string $apiKey API Key.
6566
* @param boolean $debug Log requests to debugger.
6667
* @param string $debugLogFileLocation Path and filename of debug file to write to.
6768
*/
6869
public function __construct(
69-
string $clientID,
70-
string $clientSecret,
70+
string $clientID = '',
71+
string $clientSecret = '',
7172
string $accessToken = '',
73+
string $apiKey = '',
7274
bool $debug = false,
7375
string $debugLogFileLocation = ''
7476
) {
7577
$this->client_id = $clientID;
7678
$this->client_secret = $clientSecret;
7779
$this->access_token = $accessToken;
80+
$this->api_key = $apiKey;
7881
$this->debug = $debug;
7982

8083
// Set the Guzzle client.
@@ -122,22 +125,35 @@ private function create_log(string $message)
122125
return;
123126
}
124127

125-
// Mask the Client ID, Client Secret and Access Token.
126-
$message = str_replace(
127-
$this->client_id,
128-
str_repeat('*', (strlen($this->client_id) - 4)) . substr($this->client_id, - 4),
129-
$message
130-
);
131-
$message = str_replace(
132-
$this->client_secret,
133-
str_repeat('*', (strlen($this->client_secret) - 4)) . substr($this->client_secret, - 4),
134-
$message
135-
);
136-
$message = str_replace(
137-
$this->access_token,
138-
str_repeat('*', (strlen($this->access_token) - 4)) . substr($this->access_token, - 4),
139-
$message
140-
);
128+
// Mask the Client ID, Client Secret, Access Token, and API Key.
129+
if ($this->client_id) {
130+
$message = str_replace(
131+
$this->client_id,
132+
str_repeat('*', (strlen($this->client_id) - 4)) . substr($this->client_id, - 4),
133+
$message
134+
);
135+
}
136+
if ($this->client_secret) {
137+
$message = str_replace(
138+
$this->client_secret,
139+
str_repeat('*', (strlen($this->client_secret) - 4)) . substr($this->client_secret, - 4),
140+
$message
141+
);
142+
}
143+
if ($this->access_token) {
144+
$message = str_replace(
145+
$this->access_token,
146+
str_repeat('*', (strlen($this->access_token) - 4)) . substr($this->access_token, - 4),
147+
$message
148+
);
149+
}
150+
if ($this->api_key) {
151+
$message = str_replace(
152+
$this->api_key,
153+
str_repeat('*', (strlen($this->api_key) - 4)) . substr($this->api_key, - 4),
154+
$message
155+
);
156+
}
141157

142158
// Mask email addresses that may be contained within the message.
143159
$message = preg_replace_callback(
@@ -427,7 +443,12 @@ public function get_request_headers(string $type = 'application/json', bool $aut
427443
}
428444

429445
// Add authorization header and return.
430-
$headers['Authorization'] = 'Bearer ' . $this->access_token;
446+
if ($this->api_key) {
447+
$headers['X-Kit-Api-Key'] = $this->api_key;
448+
} else if ($this->access_token) {
449+
$headers['Authorization'] = 'Bearer ' . $this->access_token;
450+
}
451+
431452
return $headers;
432453
}
433454

src/ConvertKit_API_Traits.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ trait ConvertKit_API_Traits
3333
*/
3434
protected $access_token = '';
3535

36+
/**
37+
* API Key
38+
*
39+
* @var string
40+
*/
41+
protected $api_key = '';
42+
3643
/**
3744
* OAuth Authorization URL
3845
*

0 commit comments

Comments
 (0)