devhelp / piwik-api
提供自包含的 Piwik 方法,能够使用预定义或/和运行时参数调用 API
1.0
2015-12-05 14:28 UTC
Requires
- php: >=5.3
- psr/http-message: 1.*
Requires (Dev)
Suggests
- devhelp/piwik-api-guzzle: in order to use Guzzle-based PiwikClient
This package is not auto-updated.
Last update: 2024-09-19 15:17:23 UTC
README
目的
帮助创建自包含的 Piwik 方法,能够使用预定义或/和运行时参数调用 Piwik API。帮助使用 Piwik 分段 并在方法调用时延迟加载 API 参数值。
安装
$ composer require devhelp/piwik-api
请访问 Composer 网站 获取更多信息。
用法
要使用 Method
,您需要实现 PiwikClient
类。已经实现了一个 PiwikGuzzleClient
,您需要配置 Guzzle
HTTP 客户端。
您可以通过在 composer.json 中添加 devhelp/piwik-api-guzzle 来包含 PiwikGuzzleClient
独立方法使用
$myPiwikClient = new MyPiwikClient(); $method = new Method($myPiwikClient, 'http://my.piwik.pro', 'MyModule.myAction') $method->call(array('token_auth' => $myPiwikToken));
使用 API 创建多个方法
$myPiwikClient = new MyPiwikClient(); $api = new Api($myPiwikClient, 'http://my.piwik.pro'); $api->setDefaultParams(array( 'token_auth' => $myPiwikToken, )); $api->getMethod('MyModule.myAction')->call(); $api->getMethod('MyOtherModule.myOtherAction')->call(); $api->getMethod('MyXXXModule.myXXXAction')->call();
传递参数到调用
这可以通过在方法调用时传递一个数组或将它设置为方法的默认参数或整个 API 的默认参数来完成。参数可以是标量、回调或实现 Param
接口的对象。
当参数值实现 Param
接口或是一个回调时,它的最终值是在调用()运行时解决的(导致延迟加载的参数值)。这里有一个稍后将要解释的 Segment 参数。延迟加载特别适用于返回当前登录用户的 token_auth。
$params = array( 'myCustomVar1' => 'someValue', 'myCustomVar2' => function() {/*..*/}, 'token_auth' => new LazyTokenAuthValue() ); $api->getMethod('MyModule.myAction')->call($params); /* * params to which $params array will be resolved on method call are: * array( * 'myCustomVar1' => 'someValue', * 'myCustomVar2' => ..., //this what was resolved from anonymous function * 'token_auth' => ..., //this what was resolved from LazyTokenAuthValue * ); */
使用分段
Segment
参数有自己的实现,允许构建 Piwik 分段查询。它的值是在调用时解决的
use Devhelp\Piwik\Api\Param\Segment as SegmentParam; use Devhelp\Piwik\Api\Param\Segment\Segment; $segment = new Segment(); $segment->where(new Equals('country', 'PL')) ->andWhere(new NotEquals('actions', 1)) ->andWhere(new Contains('referrerName', 'piwik')) ->orWhere(new DoesNotContain('referrerKeyword', 'myBrand')); $params = array('segment' => new SegmentParam($segment)); /* * this will be resolved to * array('segment' => 'country==PL;actions!=1;referrerName=@piwik,referrerKeyword!@myBrand'); */
集成
致谢
由: devhelp.pl 提供