简体   繁体   中英

Extending piwik to access all request data

To write a custom piwik plugin I'm following tutorial : http://piwik.org/blog/2014/09/create-widget-introducing-piwik-platform/

How can I access the request data that piwik receives within the plugin ?

Sample widget from above link :

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();
    }
}

How to access within the plugin the data piwik receives for each visitor request such as the request header fields ?

Access to a variable with name 'imageId':

$imageId = Common::getRequestVar('imageId');

About headers: Piwik provides a list of headers methods through a ProxyHeaders class. For now there are only two public static methods, that could be potentially interesting for you: ProxyHeaders::getProxyClientHeaders , working with

'HTTP_CF_CONNECTING_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',

and ProxyHeaders::getProxyHostHeaders for

'HTTP_X_FORWARDED_HOST'

Both these methods call another one, which is private:

/**
 * 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;
}

Because method getHeaders is private and it actually doesn't do what you want, probably the easiest way would be just to read headers directly from $_SERVER .

It will work this way: if you have a header with name "my-test-header" and value "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"

etc.

One note about web servers, whether it's Apache or Nginx or any other one, the configuration really matters here, especially for a HTTP_X_FORWARDED_FOR header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM