convertkit/convertkitapi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

convertkit/convertkitapi

Composer 安装命令:

composer require convertkit/convertkitapi

包简介

Kit PHP SDK for the Kit API

README 文档

README

The Kit PHP SDK provides convinient access to the Kit API from applications written in the PHP language.

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

Version Guidance

SDK Version API Version API Authentication PHP Version
1.x v3 API Key and Secret 7.4+
2.x v4 OAuth 8.0+
2.2+ v4 API Key 8.0+

Refer to this guide for changes when upgrading to the v2 SDK.

Composer

You can install this PHP SDK via Composer. Run the following command:

composer require convertkit/convertkitapi

To use the PHP SDK, use Composer's autoload:

require_once 'vendor/autoload.php';

Dependencies

The PHP SDK require the following extensions in order to work properly:

  • curl, although you can use your own non-cURL client if you prefer
  • json
  • mbstring (Multibyte String)

If you use Composer, these dependencies should be handled automatically.

Getting Started

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

First, register your OAuth application in the OAuth Applications section at https://app.kit.com/account_settings/advanced_settings.

Using the supplied Client ID and secret, redirect the user to Kit to grant your application access to their Kit account.

// Require the autoloader (if you're using a PHP framework, this may already be done for you).
require_once 'vendor/autoload.php';

// Initialize the API class.
$api = new \ConvertKit_API\ConvertKit_API(
    clientID: '<your_oauth_client_id>',
    clientSecret: '<your_oauth_client_secret>'
);

// Redirect to begin the OAuth process.
header('Location: '.$api->get_oauth_url('<your_redirect_uri>'));

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:

your-redirect-uri?code=<auth_code>

At this point, your application needs to exchange the authorization code for an access token and refresh token.

$result = $api->get_access_token(
    authCode: '<auth_code>',
    redirectURI: '<your_redirect_uri>'
);

$result is an array comprising of:

  • access_token: The access token, used to make authenticated requests to the API
  • refresh_token: The refresh token, used to fetch a new access token once the current access token has expired
  • created_at: When the access token was created
  • expires_in: The number of seconds from created_at that the access token will expire

Once you have an access token, re-initialize the API class with it:

// Initialize the API class.
$api = new \ConvertKit_API\ConvertKit_API(
    clientID: '<your_oauth_client_id>',
    clientSecret: '<your_oauth_client_secret>',
    accessToken: '<your_access_token>'
);

To refresh an access token:

$result = $api->refresh_token(
    refreshToken: '<your_refresh_token>',
    redirectURI: '<your_redirect_uri>'
);

$result is an array comprising of:

  • access_token: The access token, used to make authenticated requests to the API
  • refresh_token: The refresh token, used to fetch a new access token once the current access token has expired
  • created_at: When the access token was created
  • expires_in: The number of seconds from created_at that the access token will expire

Once you have refreshed the access token i.e. obtained a new access token, re-initialize the API class with it:

// Initialize the API class.
$api = new \ConvertKit_API\ConvertKit_API(
    clientID: '<your_oauth_client_id>',
    clientSecret: '<your_oauth_client_secret>',
    accessToken: '<your_new_access_token>'
);

API requests may then be performed:

$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');

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

$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');
$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.

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

$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@kit.com');
$api->getResponseInterface()->hasHeader('Content-Length'); // Check if the last API request included a `Content-Length` header

2.2+ (v4 API, API Key, PHP 8.0+)

Get your Kit API Key and API Secret here and set it somewhere in your application.

// Require the autoloader (if you're using a PHP framework, this may already be done for you).
require_once 'vendor/autoload.php';

// Initialize the API class.
$api = new \ConvertKit_API\ConvertKit_API(
    apiKey: '<your_v4_api_key>'
);

1.x (v3 API, API Key and Secret, PHP 7.4+)

Get your Kit API Key and API Secret here and set it somewhere in your application.

// Require the autoloader (if you're using a PHP framework, this may already be done for you).
require_once 'vendor/autoload.php';

// Initialize the API class.
$api = new \ConvertKit_API\ConvertKit_API('<your_public_api_key>', '<your_secret_api_key>');

Handling Errors

The Kit PHP SDK uses Guzzle for all HTTP API requests. Errors will be thrown as Guzzle's ClientException (for 4xx errors), or ServerException (for 5xx errors).

try {
    $forms = $api->add_subscriber_to_form('invalid-form-id');
} catch (GuzzleHttp\Exception\ClientException $e) {
    // Handle 4xx client errors.
    die($e->getMessage());
} catch (GuzzleHttp\Exception\ServerException $e) {
    // Handle 5xx server errors.
    die($e->getMessage());
}

For a more detailed error message, it's possible to fetch the API's response when a ClientException is thrown:

// Errors will be thrown as Guzzle's ClientException or ServerException.
try {
    $forms = $api->form_subscribe('invalid-form-id');
} catch (GuzzleHttp\Exception\ClientException $e) {
    // Handle 4xx client errors.
    // For ClientException, it's possible to inspect the API's JSON response
    // to output an error or handle it accordingly.
    $error = json_decode($e->getResponse()->getBody()->getContents());
    die($error->message); // e.g. "Entity not found".
} catch (GuzzleHttp\Exception\ServerException $e) {
    // Handle 5xx server errors.
    die($e->getMessage());
}

Documentation

See the PHP SDK docs

Contributing

See our contributor guide for setting up your development environment, testing and submitting a PR.

For Kit, refer to the deployment guide on how to publish a new release.

convertkit/convertkitapi 适用场景与选型建议

convertkit/convertkitapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.22k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2019 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「Forms」 「tags」 「kit」 「subscribers」 「emails」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 convertkit/convertkitapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 convertkit/convertkitapi 我们能提供哪些服务?
定制开发 / 二次开发

基于 convertkit/convertkitapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 74.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 29
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 21
  • Watchers: 3
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: GPLv3
  • 更新时间: 2019-01-14