2016-03-08 41 views
2

要編寫自定義插件piwik我下面的教程:http://piwik.org/blog/2014/09/create-widget-introducing-piwik-platform/擴展piwik訪問所有請求數據

我如何可以訪問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

訪問變量名稱爲 '圖像標識':

$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中讀取標題。

它會以這種方式工作:如果你有一個名爲 「我的測試頭」 和值 「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頭。

+0

您發佈的代碼是否需要包含在插件中? –

+0

@ blue-sky我的帖子中的方法是唯一可以與標題一起工作的方法,但由於它不能解決您的任務,您可以通過此調用直接從插件獲取標題'$ _SERVER ['HTTP_MY_TEST_HEADER'] ' – Axalix

+0

您發佈的代碼可以在插件之外運行? –