campaignmonitor/createsend-php
Composer 安装命令:
composer require campaignmonitor/createsend-php
包简介
A php library which implements the complete functionality of the Campaign Monitor API.
README 文档
README
A PHP library which implements the complete functionality of the Campaign Monitor API.
Installation
Composer
If you use Composer, you can run the following command from the root of your project:
composer require campaignmonitor/createsend-php
Or add campaignmonitor/createsend-php to your composer.json file:
{
"require": {
"campaignmonitor/createsend-php": "{version}"
}
}
Followed by running:
composer update
Manual Installation
Otherwise you can simply download the library and include it in your project.
After you have installed the library, simply include the relevant API class, as follows:
require_once 'csrest_campaigns.php'
Authenticating
The Campaign Monitor API supports authentication using either OAuth or an API key.
Using OAuth
Depending on the environment you are developing in, you may wish to use a PHP OAuth library to get access tokens for your users. If you don't use an OAuth library, you will need to get access tokens for your users by following the instructions included in the Campaign Monitor API documentation. This package provides functionality to help you do this, as described below. You may also wish to reference this example application, which is implemented using Slim but could easily be adapted for use with any PHP framework.
The first thing your application should do is redirect your user to the Campaign Monitor authorization URL where they will have the opportunity to approve your application to access their Campaign Monitor account. You can get this authorization URL by using the CS_REST_General::authorize_url() method, like so:
require_once 'csrest_general.php'; $authorize_url = CS_REST_General::authorize_url( 'Client ID for your application', 'Redirect URI for your application', 'The permission level your application requires', 'Optional state data to be included' ); # Redirect your users to $authorize_url.
If your user approves your application, they will then be redirected to the redirect_uri you specified, which will include a code parameter, and optionally a state parameter in the query string. Your application should implement a handler which can exchange the code passed to it for an access token, using CS_REST_General::exchange_token() like so:
require_once 'csrest_general.php'; $result = CS_REST_General::exchange_token( 'Client ID for your application', 'Client Secret for your application', 'Redirect URI for your application', 'A unique code for your user' # Get the code parameter from the query string ); if ($result->was_successful()) { $access_token = $result->response->access_token; $expires_in = $result->response->expires_in; $refresh_token = $result->response->refresh_token; # Save $access_token, $expires_in, and $refresh_token. } else { echo 'An error occurred:\n'; echo $result->response->error.': '.$result->response->error_description."\n"; # Handle error... }
At this point you have an access token and refresh token for your user which you should store somewhere convenient so that your application can look up these values when your user wants to make future Campaign Monitor API calls.
Once you have an access token and refresh token for your user, you can authenticate and make further API calls like so:
require_once 'csrest_general.php'; $auth = array( 'access_token' => 'your access token', 'refresh_token' => 'your refresh_token'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); var_dump($result->response);
All OAuth tokens have an expiry time, and can be renewed with a corresponding refresh token. If your access token expires when attempting to make an API call, you will receive an error response, so your code should handle this. Here's an example of how you could do this:
require_once 'csrest_general.php'; $auth = array( 'access_token' => 'your access token', 'refresh_token' => 'your refresh token' ); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); if (!$result->was_successful()) { # If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { list($new_access_token, $new_expires_in, $new_refresh_token) = $wrap->refresh_token(); # Save $new_access_token, $new_expires_in, and $new_refresh_token } # Make the call again $result = $wrap->get_clients(); } var_dump($result->response);
Using an API key
require_once 'csrest_general.php'; $auth = array('api_key' => 'your API key'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); var_dump($result->response);
API Call Timeout
You can set your local API call timeout time in createsend-php\class\transport.php line 11, in the CS_REST_CALL_TIMEOUT variable. Currently the default is 120 secs.
Examples
Samples for creating or accessing all resources can be found in the samples directory. These samples can be used as the basis for your own application and provide an outline of the expected inputs for each API call.
Further documentation of the inputs and outputs of each call can be found in the documentation in each of the csrest_*.php files or simply by examining the var_dump results in each of the provided samples.
Contributing
Please check the guidelines for contributing to this repository.
Releasing
Please check the instructions for releasing this library.
campaignmonitor/createsend-php 适用场景与选型建议
campaignmonitor/createsend-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.03M 次下载、GitHub Stars 达 283, 最近一次更新时间为 2012 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「monitor」 「campaign」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 campaignmonitor/createsend-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 campaignmonitor/createsend-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 campaignmonitor/createsend-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for interacting with the v2 MailChimp REST API
Symfony bundle to monitor and execute commands
Collections of statsd collectors with templated metric names
Symfony bundle for integrating the ZfrMailChimp library
A Laravel package to monitor queue jobs.
Zend Framework 2 module for interacting with the v2 MailChimp API, built on top of ZfrMailChimp
统计信息
- 总下载量: 6.03M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 289
- 点击次数: 19
- 依赖项目数: 37
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2012-10-19