cpa/tds
Composer 安装命令:
composer require cpa/tds
包简介
The simple php traffic direction system
README 文档
README
composer require cpa/tds /path/to/project
Add library
<?php require '/path/to/project/vendor/autoload.php';
Using
<?php $body = file_get_contents('php://input'); $request = new \Cpa\TDS\Core\Request($_SERVER, $_GET, $body, $_COOKIE); $tds = new Cpa\TDS\TDS(); $tds->click($request)->response()->out();
Configure
<?php $config = new \Cpa\TDS\Config\Config(); /* * ------------------------------------ * Paths * ------------------------------------ */ $config->path->links = '/path/to/links'; // path to compiled links classes. By default /path/to/project/data/links $config->path->responses = '/path/to/responses'; // path to compiled responses classes. By default /path/to/project/data/responses $config->path->tmp = '/path/to/temporary'; // path to compiled links classes. By default /tmp /* * ------------------------------------ * Key (define link id). * For example, link key = test * ------------------------------------ */ /* * in path * http://site.com/link/test/some/path/positions * ^ ^ ^ ^ ^ * | | | | | * position: 0 1 2 3 4 * */ $config->key->in = 'path'; $config->key->position = 1; /* * in query * http//site.com/go.php?query1=test&query2=2&query3=some-value * ^ ^ ^ * | | | * position: query1 query2 query3 */ $config->key->in = 'query'; $config->key->position = 'query1'; /* * in subdomain * http://tets.site.com/some/path */ $config->key->in = 'uri'; $config->key->position = 'host'; $config->key->pattern = '/^(?<link>[a-z](a-z0-9\-)?)\./ui'; $config->key->match = 'link'; /* * ------------------------------------ * Traffic back (default response) * ------------------------------------ */ $config->trafficBack->status = 301; $config->trafficBack->headers = ['location: https://google.com', 'content-type: text/plain']; $config->trafficBack->body = 'redirect to google'; $tds = new \Cpa\TDS\TDS($config);
Adding responses
<?php // save from json $json = <<<JSON { "key": "e404", "status": 404, "headers": [ "content-type: text/plain" ], "body": "Not Found" } JSON; $tds->storage()->response()->save($json); // save from file file_put_contents('/path/to/project/data/responses/e404.json', $json); $tds->storage()->response()->save('/path/to/project/data/responses/e404.json');
Adding links
<?php // save from json $json = <<<JSON { "key": "test", "responses": [ "e404" ] } JSON; $tds->storage()->link()->save($json); // save from file file_put_contents('/path/to/project/data/links/test.json', $json); $tds->storage()->response()->save('/path/to/project/data/links/test.json');
AB tests
<?php // response A $responseA = <<<JSON { "key": "response-a", "status": 301, "headers": [ "location: https:\/\/site.com\/a", "content-type: text/plain" ], "body": "redirect to https:\/\/site.com\/a" } JSON; // response B $responseB = <<<JSON { "key": "response-b", "status": 301, "headers": [ "location: https:\/\/site.com\/b", "content-type: text/plain" ], "body": "redirect to https:\/\/site.com\/b" } JSON; // test link $link = <<<JSON { "key": "test", "responses": [ "response-a" "response-b" ] } JSON; $tds->storage()->response()->save($responseA); $tds->storage()->response()->save($responseB); $tds->storage()->link()->save($link); /* * you nueno save links and responses ONLY when changing */
Traffic Rules
<?php /* * You can set any parameters of request and set rules by this parameters * By defaults, request have a this parameters: * * header.{HEADER_NAME} = {HEADER_VALUE} * cookie.{COOKIE_NAME} = {COOKIE_VALUE} * ip = $_SERVER['REMOTE_ADDR'] * uri.scheme = https OR http * uri.host = {HOST_OF_REQUEST} (for example, site.com) * uri.port = {PORT_OF_REQUEST} (for example, 8081) * uri.path = {PATH_OF_REQUEST} (for example, /link/go.php) * uri.query = {QUERY_STRING_OF_REQUEST} (for.example, link=test¶meter=value) * uri = {URI_FULL_STRING} (for example, http://site.com:8081/link/go.php?link=test¶meter=value) * query.{QUERY_PARAM_NAME} = {QUERY_PARAM_VALUE} * path.{NUMBER_OF_PATH} = {NAME_OF_PATH} * method = $_SERVER['REQUEST_METHOD'] (may be GET, POST, PUT, DELETE, OPTIONS, HEAD etc) * body = {BODY_STRING} * body.{PATH}.{TO}.{KEY} = {VALUE} (for content-types application/json and application/x-www-form-urlencoded) * */ $extends = array( 'device' => array( 'type' => 'phone' ), ); $body = file_get_contents('php://input'); $request = new \Cpa\TDS\Core\Request($_SERVER, $_GET, $body, $_COOKIE, $extends); $link = <<<JSON { "key": "test", "rules": [ "criteria": [ { "parameter": "device.type", "operator": "is", "values": ["phone", "tablet"] } ], "responses": [ "response-a" ] ], "responses": [ "response-b" ] } JSON; $tds->storage()->link()->save($link);
Tokens
<?php /* * You can set tokens for request and set it in rules and responses. */ $body = file_get_contents('php://input'); $request = new \Cpa\TDS\Core\Request($_SERVER, $_GET, $body, $_COOKIE); // multi language site $langSite = <<<JSON { "key": "lang-site", "status": 301, "headers": [ "location: https:\/\/{{lang}}.site.com", "content-type: text/plain" ], "body": "redirect to https:\/\/{{lang}}.site.com" } JSON; // default site $default = <<<JSON { "key": "site", "status": 301, "headers": [ "location: https:\/\/site.com", "content-type: text/plain" ], "body": "redirect to https:\/\/site.com" } JSON; $tds->storage()->response()->save($langSite); $tds->storage()->response()->save($default); // link $link = <<<JSON { "key": "test", "tokens": [ { "name": "lang", "in": "geo", "position": "country", } ], "rules": [ "criteria": [ { "parameter": "token.lang", "operator": "is", "values": ["en", "ru"] } ], "responses": [ "lang-site" ] ], "responses": [ "site" ] } JSON; $extends = array( 'geo' => array( 'country' => 'en' // You must define it ), ); $body = file_get_contents('php://input'); $request = new \Cpa\TDS\Core\Request($_SERVER, $_GET, $body, $_COOKIE, $extends); $tds->storage()->link()->save($link);
Deleting responses and links
<?php $tds->storage()->response()->delete('response-a'); $tds->storage()->link()->delete('test');
Metrics
<?php $body = file_get_contents('php://input'); $request = new \Cpa\TDS\Core\Request($_SERVER, $_GET, $body, $_COOKIE); $tds = new Cpa\TDS\TDS(); $click = $tds->click($request); /* * You can process the request and response data * $click->response()->key() - key of response * $click->response()->body() - body of response * $click->response()->headers(false) - array of response headers (for example, array('location: http://site.com')) * $click->response()->headers(true) - assoc array of response headers (for example, array('location' => 'http://site.com')) * $click->response()->status() - status of response * (string)$request - raw data of request * (string)$click->response() - raw data of response * $click->criteria() - criteria of current rule * $click->link() - link key * $click->tokens() - associative array of tokens */ $click->response()->out();
cpa/tds 适用场景与选型建议
cpa/tds 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「cpa」 「tds」 「traffic-direction-system」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cpa/tds 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cpa/tds 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cpa/tds 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Keitaro Client: Click API, Admin API
Laravel CPA (Cost Per Action) network integration package
API Wrapper for TDS services
PHP lib for taxonomy
http客户端
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-27