簡體   English   中英

擴展piwik以訪問所有請求數據

[英]Extending piwik to access all request data

要編寫自定義的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為每個訪問者請求(例如請求標頭字段)接收的數據?

訪問名稱為“ 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” =>'application / x-www-form-urlencoded'

$_SERVER['HTTP_CONTENT_TYPE'] // returns "application/x-www-form-urlencoded"

等等

關於Web服務器的注釋,無論是Apache還是Nginx或任何其他服務器,此處的配置確實很重要,尤其是對於HTTP_X_FORWARDED_FOR標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM