定制 pacely/mailchimp-apiv3 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

pacely/mailchimp-apiv3

Composer 安装命令:

composer require pacely/mailchimp-apiv3

包简介

Simple API wrapper for Mailchimp API V3

README 文档

README

Installation

Add the following to your composer.json

{
    "require": {
        "pacely/mailchimp-apiv3": "dev-master"
    }
}

Laravel Users

We've added some classes to help Laravel 5 users make use of the library with ease.

Service Provider

You can register our service provider in your app.php config file.

// config/app.php
'providers' => [
    ...
    Mailchimp\MailchimpServiceProvider::class
]

Facade

If you prefer facades, make sure you add this as well:

// config/app.php
'aliases' => [
    ...
    'MC' => Mailchimp\MailchimpFacade::class
]

NOTE: Make sure not to register the facade with the Mailchimp alias, as that could potentially clash with the base class.

Configuration

There are only one configuration option you need to fill in. Publish the config by running:

php artisan vendor:publish

Now, the config file will be located under config/mailchimp.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mailchimp API key
    |--------------------------------------------------------------------------
    |
    | To obtain an API key, go to mailchimp.com under your profile
    | you will find Extras -> API keys. Paste the key below.
    |
    */
    'apikey' => ''
];

Usage

There's one method to rule them all:

request($resource, $arguments = [], $method = 'GET') // $arguments is used as POST data or GET parameters, depending on the method used.

But its clever enough to map these calls aswell:

get($resource, array $options = [])
head($resource, array $options = [])
put($resource, array $options = [])
post($resource, array $options = [])
patch($resource, array $options = [])
delete($resource, array $options = [])

Pagination

We use offset and count in the query string to paginate data, because it provides greater control over how you view your data. Offset defaults to 0, so if you use offset=1, you'll miss the first element in the dataset. Count defaults to 10.

Source: http://kb.mailchimp.com/api/article/api-3-overview

Filtering

Most endpoints don't currently support filtering, but we plan to add these capabilities over time. Schemas will tell you which collections can be filtered, and what to include in your query string.

Source: http://kb.mailchimp.com/api/article/api-3-overview

Partial Response

To cut down on data transfers, pass a comma separated list of fields to include or exclude from a certain response in the query string. The parameters fields and exclude_fields are mutually exclusive and will throw an error if a field isn't valid in your request.

Source: http://kb.mailchimp.com/api/article/api-3-overview

Behind Proxy

If you are behind a proxy, you can use setProxy directly on the class.

setProxy(host : string, port : int, [ssl : bool = false], [username = null], [password = null]);

See the example.

Examples

Collection object

All queries will return an instance of the Illuminate\Support\Collection object, which is really easy to work with. If you don't want to use the Collection object however, you can transform it into an array using $result->toArray().

$mc = new Mailchimp('<api-key>', '<guzzle-options[array]>');

// Get 10 lists starting from offset 10 and include only a specific set of fields
$result = $mc->request('lists', [
    'fields' => 'lists.id,lists.name,lists.stats.member_count',
    'offset' => 10,
    'count' => 10
]);

// Will fire this query: 
// GET https://us1.api.mailchimp.com/3.0/lists?fields=lists.id,lists.name,lists.stats.member_count&count=10

// Returns object(Illuminate\Support\Collection)
var_dump($result);

// Returns the first item
var_dump($result->first());

// Returns 3 items
var_dump($result->take(3));

// Returns a JSON string
var_dump($result->toJson());

// Returns an array
var_dump($result->toArray());

You can use a simple foreach/for loop or use the built in each(callable $callback) provided by our Collection object to loop through your items.

$result->each(function ($item) {
    echo $item['name'].' ('.$item['stats']['member_count'].')'.PHP_EOL;
});

There's alot more you can do with the Collection object.

Create lists

// All these fields are required to create a new list.
$result = $mc->post('lists', [
    'name' => 'New list',
    'permission_reminder' => 'You signed up for updates on Greeks economy.',
    'email_type_option' => false,
    'contact' => [
        'company' => 'Doe Ltd.',
		'address1' => 'DoeStreet 1',
		'address2' => '',
		'city' => 'Doesy',
		'state' => 'Doedoe',
		'zip' => '1672-12',
		'country' => 'US',
		'phone' => '55533344412'
    ],
    'campaign_defaults' => [
        'from_name' => 'John Doe',
        'from_email' => 'john@doe.com',
        'subject' => 'My new campaign!',
        'language' => 'US'
    ]
]);

Subresources

$result = $mc->get('lists/e04d611199', [
    'fields' => 'id,name,stats.member_count'
]);

Proxy

$mc->setProxy('https://127.0.0.1', 10, true, 'username', 'password');

$result = $mc->get('lists/e04d611199', [
    'fields' => 'id,name,stats.member_count'
]);

Further documentation

You should read through Mailchimp's API v3 documentation (I know, it's pretty rough. Should get better soon.). To find out which resources is available, take a look at the JSON API Schema for Mailchimp.

pacely/mailchimp-apiv3 适用场景与选型建议

pacely/mailchimp-apiv3 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 658.79k 次下载、GitHub Stars 达 95, 最近一次更新时间为 2015 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pacely/mailchimp-apiv3 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 658.79k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 97
  • 点击次数: 15
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 95
  • Watchers: 10
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-11