printu/customerio
Composer 安装命令:
composer require printu/customerio
包简介
PHP API for Customer.io
README 文档
README
PHP bindings for the Customer.io API.
There are two primary API hosts available for to integrate with:
Behavioral Tracking
https://track.customer.io/api/v1/
Behavioral Tracking API is used to identify and track customer data with Customer.io.
API
https://api.customer.io/v1/api/
API allows you to read data from your Customer.io account for use in custom workflows in your backend system or for reporting purposes.
Installation
The API client can be installed via Composer.
In your composer.json file:
{
"require": {
"printu/customerio": "~3.0"
}
}
Once the composer.json file is created you can run composer install for the initial package install and composer update to update to the latest version of the API client.
The client uses Guzzle.
Basic Usage
Remember to include the Composer autoloader in your application:
<?php require_once 'vendor/autoload.php'; // Application code... ?>
Configure your access credentials when creating a client:
<?php use Customerio\Client; $client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID'); /* * To authenticate, provide your key as a Bearer token in a HTTP Authorization header. * You can create and manage your API keys by visiting your App API Keys page directly or by clicking the Integrations * link in the left-hand menu of your Customer.io account and choosing Customer.io API > Manage API Credentials > App API Keys. */ $client->setAppAPIKey('APP_KEY'); ?>
Change region to EU
<?php use Customerio\Client; $client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID', ['region' => 'eu']); ?>
Local Testing
Run phpunit from the project root to start all tests.
Examples
Customers
<?php // Create customer try { $client->customers->add( [ 'id' => 1, 'email' => 'user@example.com', 'plan' => 'free', 'created_at' => time() ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error } // Get customer try { $client->customers->get( [ 'email' => 'user@example.com', ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error } // Update customer try { $client->customers->update( [ 'id' => 1, 'email' => 'user@example.com', 'plan' => 'premium' ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error } // Delete customer try { $client->customers->delete( [ 'id' => 1, ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Events
<?php // Add customer event try { $client->customers->event( [ 'id' => 1, 'name' => 'test-event', 'data' => [ 'event-metadata-1' => 'test', 'event-metadata-2' => 'test-2' ] ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error } // Add anonymous event try { $client->events->anonymous( [ 'name' => 'invite-friend', 'data' => [ 'recipient' => 'invitee@example.com' ] ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Anonymous event example usage.
Segments
<?php // Get segment data try { $client->segments->get( [ 'id' => 1 ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Check for other available methods here
PageView
<?php // Add page view try { $result = $client->page->view( [ 'id' => 1, 'url' => 'http://example.com/login', 'data' => [ 'referrer' => 'http://example.com' ] ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Campaigns
<?php // Get campaigns data try { $client->campaigns->get( [ 'id' => 1 ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Check for other available methods here
<?php // Trigger broadcast campaign try { $result = $client->campaigns->trigger( [ 'id' => 1, 'data' => [ 'headline' => 'Roadrunner spotted in Albuquerque!', 'date' => 'January 24, 2018', 'text' => 'We\'ve received reports of a roadrunner in your immediate area! Head to your dashboard to view more information!' ], 'recipients' => [ 'segments' => [ 'id' => 1 ] ] ] ); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
See here for more examples of API Triggered Broadcasts
V2 Track API
The Track API allows you to send entity-based operations to Customer.io. You can use it for both single operations and batch operations.
Note: The
identifyaction is used to create or update an entity.
Single Entity Operation
Create or update a person entity.
<?php // Single entity operation try { $client->track->entity([ 'type' => 'person', 'action' => 'identify', 'identifiers' => [ 'id' => '123' // or 'email' => 'test@example.com' or 'cio_id' => 'cio_123' ], 'attributes' => [ 'name' => 'John Doe', 'plan' => 'premium', 'test_attribute' => null, // pass null to remove the attribute from the entity 'last_activity_at' => time() ] ]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Create or update an object entity.
<?php // Create or update an object try { $client->track->entity([ 'type' => 'object', 'action' => 'identify', 'identifiers' => [ 'object_type_id' => 'product', 'object_id' => 'SKU-123' ], 'attributes' => [ 'name' => 'Awesome Product', 'price' => 99.99, 'category' => 'Electronics' ] ]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Delete an entity
<?php // Delete an entity try { $client->track->entity([ 'type' => 'person', 'action' => 'delete', 'identifiers' => [ 'id' => '123' ] ]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Add relationships to a person
<?php // Add relationships to a person entity try { $client->track->entity([ 'type' => 'person', 'action' => 'add_relationships', 'identifiers' => [ 'id' => '123' ], 'cio_relationships' => [ 'identifiers' => [ 'object_type_id' => 'product', 'object_id' => 'SKU-123' ], 'relationship_attributes' => [ 'role' => 'client', 'created_at' => '2024-01-01T10:12:00Z' ] ] ]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Batch Operations
<?php // Batch multiple operations try { $client->track->batch([ 'batch' => [ [ 'type' => 'person', 'action' => 'identify', 'identifiers' => ['id' => '123'], 'attributes' => ['name' => 'John Doe'] ], [ 'type' => 'person', 'action' => 'event', 'identifiers' => ['id' => '123'], 'name' => 'purchased', 'timestamp' => time(), 'attributes' => ['product_id' => 'SKU-123'] ], [ 'type' => 'object', 'action' => 'identify', 'identifiers' => [ 'object_type_id' => 'product', 'object_id' => 'SKU-123' ], 'attributes' => ['in_stock' => true] ] ] ]); } catch (\GuzzleHttp\Exception\GuzzleException $e) { // Handle the error }
Main differences with V1
The Track V2 API introduces an entity-centric approach, contrasting with V1's action-based model. The key difference lies in the request structure:
- V1 API: Actions come first (identify, track event) through the endpoint followed by the target entity you provide.
- V2 API: The target entity type comes first (person, object) followed by the action to perform.
This new approach provides a more intuitive way to interact with your data by focusing first on what entity you're working with before specifying what you want to do with it. It also allows the API to have only two endpoint for all operations.
In theory, all v1 operations can be done with v2 but the v2 API does not support all v1 operations yet.
For more details, see the Track V2 API documentation.
License
MIT license. See the LICENSE file for more details.
printu/customerio 适用场景与选型建议
printu/customerio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21M 次下载、GitHub Stars 达 24, 最近一次更新时间为 2015 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「rest」 「api」 「client」 「Customerio」 「customer.io」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 printu/customerio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 printu/customerio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 printu/customerio 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
http客户端
Easily add Excepct-CT header to your project
Build mini web servers in PHP for testing
统计信息
- 总下载量: 1.21M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 23
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-13