Skip to content

Commit 7a379ac

Browse files
committed
initial commit
0 parents  commit 7a379ac

5 files changed

Lines changed: 994 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

ConvertKit.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
namespace Firewards\Apis\ConvertKit;
3+
4+
use ConvertKit_API\ConvertKit_API;
5+
6+
/**
7+
* Badges
8+
*
9+
* @author Dennis Stücken
10+
* @license proprietary
11+
* @copyright firewards.com
12+
* @link https://www.firewards.com
13+
*
14+
* @version $Version$
15+
* @package DS\Model
16+
*
17+
* @method static findFirstById(int $id)
18+
*/
19+
class ConvertKit extends ConvertKit_API
20+
{
21+
/**
22+
* @param int $page
23+
*
24+
* @return false|mixed
25+
*/
26+
public function getSubscribers($page = 1, $sort_order = null, $from = null, $to = null, $updated_from = null, $updated_to = null, $email_address = null)
27+
{
28+
29+
$request = $this->api_version . '/subscribers';
30+
31+
$options = [
32+
'api_secret' => $this->api_secret,
33+
];
34+
35+
if ($page)
36+
{
37+
$options['page'] = $page;
38+
}
39+
40+
if ($from)
41+
{
42+
$options['from'] = $from;
43+
}
44+
45+
if ($to)
46+
{
47+
$options['to'] = $to;
48+
}
49+
50+
if ($updated_from)
51+
{
52+
$options['updated_from'] = $updated_from;
53+
}
54+
55+
if ($updated_to)
56+
{
57+
$options['updated_to'] = $updated_to;
58+
}
59+
60+
if ($sort_order)
61+
{
62+
$options['sort_order'] = $sort_order;
63+
}
64+
65+
if ($email_address)
66+
{
67+
$options['email_address'] = $email_address;
68+
}
69+
70+
return $this->make_request($request, 'GET', $options);
71+
}
72+
73+
/**
74+
* @return false|mixed
75+
*/
76+
public function getCustomFields()
77+
{
78+
$request = $this->api_version . '/custom_fields';
79+
80+
$options = [
81+
'api_key' => $this->api_key,
82+
];
83+
84+
return $this->make_request($request, 'GET', $options);
85+
}
86+
87+
/**
88+
* @param string $label
89+
*
90+
* @return false|mixed
91+
*/
92+
public function createCustomField(string $label)
93+
{
94+
$request = $this->api_version . '/custom_fields';
95+
96+
$options = [
97+
'api_secret' => $this->api_secret,
98+
'label' => $label,
99+
];
100+
101+
return $this->make_request($request, 'POST', $options);
102+
}
103+
104+
/**
105+
* @param string $subscriberId
106+
* @param array $fields
107+
*
108+
* @return false|mixed
109+
*/
110+
public function updateSubscriber(string $subscriberId, array $fields = [])
111+
{
112+
$request = $this->api_version . '/subscribers/' . $subscriberId;
113+
114+
$options = [
115+
'api_secret' => $this->api_secret,
116+
'fields' => $fields,
117+
];
118+
119+
return $this->make_request($request, 'PUT', $options);
120+
}
121+
122+
/**
123+
* @param string $name
124+
*
125+
* @return false|mixed
126+
*/
127+
public function createTag(string $name)
128+
{
129+
$request = $this->api_version . '/tags';
130+
131+
$options = [
132+
'api_secret' => $this->api_secret,
133+
'tag' => ['name' => $name],
134+
];
135+
136+
return $this->make_request($request, 'POST', $options);
137+
}
138+
139+
/**
140+
* @return false|mixed
141+
*/
142+
public function getTags()
143+
{
144+
$request = $this->api_version . '/tags';
145+
146+
$options = [
147+
'api_secret' => $this->api_secret,
148+
];
149+
150+
return $this->make_request($request, 'GET', $options);
151+
}
152+
153+
/**
154+
* @param string $tagId
155+
* @param string $email
156+
* @param string $firstName
157+
* @param array $fields
158+
*
159+
* @return false|mixed
160+
*/
161+
public function addSubscriberToTag(string $tagId, string $email, string $firstName = '', array $fields = [])
162+
{
163+
$request = $this->api_version . '/tags/' . $tagId . '/subscribe';
164+
165+
$options = [
166+
'api_secret' => $this->api_secret,
167+
'email' => $email,
168+
'first_name' => $firstName,
169+
'fields' => $fields,
170+
];
171+
172+
return $this->make_request($request, 'POST', $options);
173+
}
174+
}

README.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# ConvertKit PHP
2+
Unofficial ConvertKit PHP API for [v3](https://api.convertkit.com/v3/).
3+
4+
This package makes it simple to access ConvertKit's web API. Checkout [https://developers.convertkit.com](https://developers.convertkit.com) for more information on ConvertKit's API.
5+
6+
[![Source Code](https://img.shields.io/badge/source-convertkit--php--api-blue)](https://github.com/Firewards/convertkit-php-api)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/Firewards/convertkit-php-api/blob/master/LICENSE)
8+
9+
## Install
10+
11+
Via Composer
12+
13+
``` bash
14+
$ composer require firewards/convertkit-php-api
15+
```
16+
17+
## Requirements
18+
19+
The following versions of PHP are supported.
20+
21+
* PHP 5.6
22+
* PHP 7.0
23+
* PHP 7.1
24+
* PHP 7.2
25+
* PHP 7.3
26+
27+
### API Key
28+
All API calls require an API Key. You can find your API Key in the ConvertKit Account page.
29+
30+
### API Secret
31+
Some API calls require an API Secret. All calls that require the api key will also work with the api secret, there's no need to use both. This key grants access to sensitive data and actions on your subscribers.
32+
33+
## Usage
34+
Start by using ConvertKit API and creating an instance with your ConvertKit API key
35+
```php
36+
$api = new \Firewards\Apis\ConvertKit($api_key, $api_secret);
37+
```
38+
### Examples
39+
40+
**Get all Subscribers**
41+
42+
Get all subscribers using pagination.
43+
```php
44+
$i = 0;
45+
while ($subscribers = $api->getSubscribers($i++))
46+
{
47+
if (!isset($subscribers->subscribers) || count($subscribers->subscribers) === 0)
48+
{
49+
break;
50+
}
51+
52+
var_dump($subscribers);
53+
}
54+
```
55+
56+
**Get all Custom Fields**
57+
58+
```php
59+
$customFields = $api->getCustomFields();
60+
```
61+
62+
**Create a Custom Field**
63+
64+
```php
65+
$lastNameField = $api->createCustomField('last_name');
66+
```
67+
68+
**Update subscriber**
69+
70+
Updates a subscriber and adds info to a custom field 'last_name'.
71+
72+
```php
73+
$subscriberId = '1234';
74+
$lastNameField = $api->updateSubscriber($subscriberId, ['last_name' => 'Stücken']);
75+
```
76+
77+
**Get Tags**
78+
79+
Retrieve all tags.
80+
81+
```php
82+
$tags = $api->getTags();
83+
```
84+
85+
**Add Subscriber to Tag**
86+
87+
Adds a subscriber to a specific tag.
88+
89+
```php
90+
$added = $api->addSubscriberToTag($tagId, $email);
91+
```
92+
93+
**Subscribe to a form**
94+
95+
Add a subscriber to a form. The `$subscribed` response will be an object.
96+
97+
```php
98+
$tag_id = '99999'; // This tag must be valid for your ConvertKit account.
99+
100+
$options = [
101+
'email' => 'test@test.com',
102+
'name' => 'Full Name',
103+
'first_name' => 'First Name',
104+
'tags' => $tag_id,
105+
'fields' => [
106+
'phone' => 134567891243,
107+
'shirt_size' => 'M',
108+
'website_url' => 'testurl.com'
109+
]
110+
];
111+
112+
$subscribed = $api->form_subscribe($this->test_form_id, $options);
113+
```
114+
115+
**Get Subscriber ID**
116+
117+
Get the ConvertKit Subscriber ID for a given email address.
118+
119+
```php
120+
$subscriber_id = $api->get_subscriber_id( $email );
121+
```
122+
123+
**Get Subscriber**
124+
125+
Get subscriber data for a ConvertKit Subscriber.
126+
127+
```php
128+
$subscriber = $api->get_subscriber( $subscriber_id );
129+
```
130+
131+
**Get Subscriber Tags**
132+
133+
Get all tags applied to a Subscriber.
134+
135+
```php
136+
$subscriber_tags = $api->get_subscriber_tags( $subscriber_id );
137+
```
138+
139+
**Add Tag to a Subscriber**
140+
141+
Apply a tag to a Subscriber.
142+
143+
```php
144+
$tag_id = '99999'; // This tag must be valid for your ConvertKit account.
145+
$api->add_tag(tag_id, [
146+
'email' => 'test@test.com'
147+
]);
148+
```
149+
150+
### Rate limiting
151+
152+
Please note that ConvertKit is rate limiting requests by 120 requests per minute.
153+
If your request rate exceeds the limit, ConvertKit PHP Api will throw a *RateLimitExcededException*.
154+
155+
## Contributing
156+
157+
Please see [CONTRIBUTING](https://github.com/thephpleague/oauth2-client/blob/master/CONTRIBUTING.md) for details.
158+
159+
## License
160+
161+
The MIT License (MIT). Please see [License File](https://github.com/thephpleague/oauth2-client/blob/master/LICENSE) for more information.
162+
163+
## Sponsor
164+
165+
This package is sponsored by [www.firewards.com](https://www.firewards.com), Firewards makes it easy to setup a referral and rewards program for your email list and newsletter.

composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "firewards/convertkit-php-api",
3+
"description": "Simple access interface to ConvertKit's web API.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Dennis Stücken",
9+
"email": "dennis@firewards.com"
10+
}
11+
],
12+
"require": {
13+
"convertkit/convertkitapi": "dev-master"
14+
}
15+
}

0 commit comments

Comments
 (0)