jacobbennett / sendyphp
用于与 Sendy 新闻letter 系统交互的 PHP 库(http://sendy.co)
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
- squizlabs/php_codesniffer: 1.*
README
一个用于与 Sendy API(http://sendy.co)交互的 PHP 类
安装
使用 Composer
首先,通过 Composer 安装此包。编辑你的项目 composer.json
文件以要求 jacobbennett/sendyphp
。
"require": {
"jacobbennett/sendyphp": "1.3.*"
}
然后,从终端更新 Composer
composer update
非 Composer 安装
- 获取
src/SendyPHP.php
文件并将其放入你的文件结构中。 - 在你想使用它的位置引入 SendyPHP。
require('SendyPHP.php');
用法
创建一个实例,传递一个包含你的 API 密钥、安装 URL 和你希望与之工作的列表 ID 的数组。
$config = array( 'api_key' => 'yourapiKEYHERE', //your API key is available in Settings 'installation_url' => 'http://updates.mydomain.com', //Your Sendy installation 'list_id' => 'your_list_id_goes_here' ); $sendy = new \SendyPHP\SendyPHP($config); //you can change the list_id you are referring to at any point $sendy->setListId("a_different_list_id");
方法
创建新的 SendyPHP 实例后,可以调用以下任何方法
返回值
这些函数的返回值将包括状态和与状态相关的消息。
状态是一个布尔值 true
或 false
,消息将根据所执行的操作类型而变化。
//example of a succesful return value array( 'status'=>true, 'message'=>'Already Subscribed' ) //example of a UNsuccesful return value array( 'status'=>false, 'message'=>'Some fields are missing.' )
我已经对代码进行了注释和组织,以便于阅读,如果你对返回的状态或消息有进一步的问题,请参阅库注释。
subscribe(array $values)
此方法接受一个包含 $values
的数组,并尝试将 $values
添加到在 $list_id
中指定的列表中
$results = $sendy->subscribe(array( 'name'=>'Jim', 'email' => 'Jim@gmail.com', //this is the only field required by sendy 'customfield1' => 'customValue' ));
注意:在使用此库之前,请确保在 Sendy 中为列表添加任何自定义字段。另一个注意事项:如果用户已经订阅了列表,库将返回状态 true
。请随意修改代码以满足您的需求。
unsubscribe($email)
从当前列表中取消订阅提供的电子邮件地址(如果存在)。
$results = $sendy->unsubscribe('test@testing.com');
substatus($email)
返回具有提供的电子邮件地址的用户(如果存在)在当前列表中的状态。
$results = $sendy->substatus('test@testing.com');
注意:请参阅代码或查看 http://sendy.co/api 了解您可能期望的返回消息类型。
subcount()
返回当前列表的订阅者数量。
$results = $sendy->subcount();
createCampaign(array $values)
此方法接受一个包含 $values
的数组,并将创建一个活动(可选发送它)。
$results = $sendy->createCampaign(array( 'from_name' => 'Some Name', 'from_email' => 'some@domain.com', 'reply_to' => 'some@domain.com', 'subject' => 'Some Subject', 'plain_text' => 'Amazing campaign', // (optional). 'html_text' => '<h1>Amazing campaign</h1>', 'list_ids' => 'your_list_id', // Required only if you set send_campaign to 1. 'brand_id' => 0, // Required only if you are creating a 'Draft' campaign. 'query_string' => 'some', // eg. Google Analytics tags. 'send_campaign' => 0 // Set to 1 if you want to send the campaign as well and not just create a draft. Default is 0. ));
setListId($list_id) 和 getListId()
更改或获取你当前正在工作的列表。
//set or switch the list id $sendy->setListId('another_list_id'); //get the current list id echo $sendy->getListId();
单元测试
所有单元测试都位于 src/test 目录下。要从项目根目录运行测试,请输入以下内容。
php vendor/bin/phpunit src/test/SendyPHPTest.php
确保已设置测试的 API 密钥。
$config = [ 'api_key' => 'xxx', //your API key is available in Settings 'installation_url' => 'http://my.sendy.installation.com', //Your Sendy installation 'list_id' => 'xxx'// List ID ];