【问题标题】:Extending piwik to access all request data扩展 piwik 以访问所有请求数据
【发布时间】:2016-03-08 16:42:10
【问题描述】:

要编写自定义 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为每个访问者请求接收的数据,例如请求头字段?

【问题讨论】:

    标签: php plugins matomo


    【解决方案1】:

    访问名为“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"
    

    等等

    关于网络服务器的注意事项,无论是 Apache 还是 Nginx 或任何其他服务器,配置在这里都非常重要,尤其是对于 HTTP_X_FORWARDED_FOR 标头。

    【讨论】:

    • 您发布的代码,是否需要包含在插件中?
    • @blue-sky 我帖子中的方法是唯一适用于标头的方法,但由于它不能解决您的任务,您可以通过此调用直接从插件访问标头@ 987654334@
    • @blue-sky 绝对是,这是一种读取标题的 PHP 方式,因此可以将其放置在插件中或外部。 php.net/manual/en/reserved.variables.server.php "$_SERVER 是一个数组,包含诸如标题、路径和脚本位置等信息。此数组中的条目由 Web 服务器创建。不能保证每个 Web 服务器都会提供这些信息;服务器可能会省略一些,或提供其他未在此处列出的内容。"
    猜你喜欢
    • 2012-11-16
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2020-02-15
    • 2021-07-10
    相关资源
    最近更新 更多