要编写自定义piwik插件,我正在学习教程:http://piwik.org/blog/2014/09/create-widget-introducing-piwik-platform/
如何访问piwik在插件中收到的请求数据?
上面链接中的示例窗口小部件:
class Widgets extends \Piwik\Plugin\Widgets
{
/**
* Here you can define the category the widget belongs to. You can reuse any existing widget category or define your own category.
* @var string
*/
protected $category = 'ExampleCompany';
/**
* Here you can add one or multiple widgets. You can add a widget by calling the method "addWidget()" and pass the name of the widget as well as a method name that should be called to render the widget. The method can be defined either directly here in this widget class or in the controller in case you want to reuse the same action for instance in the menu etc.
*/
protected function init()
{
$this->addWidget('Example Widget Name', $method = 'myExampleWidget');
$this->addWidget('Example Widget 2', $method = 'myExampleWidget', $params = array('myparam' => 'myvalue'));
}
/**
* This method renders a widget as defined in "init()". It's on you how to generate the content of the widget. As long as you return a string everything is fine. You can use for instance a "Piwik\View" to render a twig template. In such a case don't forget to create a twig template (eg. myViewTemplate.twig) in the "templates" directory of your plugin.
*
* @return string
*/
public function myExampleWidget()
{
$view = new View('@MyWidgetPlugin/myViewTemplate');
return $view->render();
}
}
如何在插件中访问piwik为每个访问者请求(例如请求标题字段)收到的数据?
答案 0 :(得分:0)
访问名为'imageId'的变量:
$imageId = Common::getRequestVar('imageId');
关于标题:
Piwik通过ProxyHeaders
类提供了标题方法列表。
目前只有两种公共静态方法,可能对您有意义:
ProxyHeaders::getProxyClientHeaders
,使用
'HTTP_CF_CONNECTING_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
和ProxyHeaders::getProxyHostHeaders
'HTTP_X_FORWARDED_HOST'
这两种方法都调用另一种方法,即私有方法:
/**
* Get headers present in the HTTP request
*
* @param array $recognizedHeaders
* @return array HTTP headers
*/
private static function getHeaders($recognizedHeaders)
{
$headers = array();
foreach ($recognizedHeaders as $header) {
if (isset($_SERVER[$header])) {
$headers[] = $header;
}
}
return $headers;
}
因为方法getHeaders
是私有的,实际上并不是你想要的,所以最简单的方法就是直接从$_SERVER
读取标题。
它将以这种方式工作:如果你有一个名为“my-test-header”的标题和值“123”:
$_SERVER['HTTP_MY_TEST_HEADER'] // returns "123"
“Content-Type”=> '应用程序/ x WWW的形式进行了urlencoded'
$_SERVER['HTTP_CONTENT_TYPE'] // returns "application/x-www-form-urlencoded"
等
关于Web服务器的一个注意事项,无论是Apache还是Nginx,还是其他任何一个,配置在这里都非常重要,特别是对于HTTP_X_FORWARDED_FOR
标题。