Calling the API
This page explains the two ways to call the Piwik API to request your web analytics data: internal call or external (internet http) call
Using the REST API over http
If you want to request data in any language (php, python, ruby, asp, C++, Java, etc.) you can use the REST API. It is a simple way to request data via standard HTTP GET.
You can for example get the 100 best search engines keywords used on your website during the current week. Here is an example in PHP:
<?php // this token is used to authenticate your API request. // You can get the token on the API page inside your Piwik interface $token_auth = 'anonymous'; // we call the REST API and request the 100 first keywords for the last month for the idsite=1 $url = "http://piwik.org/demo/"; $url .= "?module=API&method=Referers.getKeywords"; $url .= "&idSite=1&period=month&date=yesterday"; $url .= "&format=PHP&filter_limit=20"; $url .= "&token_auth=$token_auth"; $fetched = file_get_contents($url); $content = unserialize($fetched); // case error if(!$content) { print("Error, content fetched = ".$fetched); } print("<h1>Keywords for the last month</h1>"); foreach($content as $row) { $keyword = urldecode($row['label']); $hits = $row['nb_visits']; print("<b>$keyword</b> ($hits hits)<br>"); } ?>
Here is the output of this code:
Error, content fetched =
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 4104 bytes) in /home/www/piwik/demo/modules/DataTable.php on line 672
Keywords for the last month
Warning: Invalid argument supplied for foreach() in /home/www/piwik/demo/misc/api_rest_call.php on line 23
From within PHP
This is the most efficient technique as it doesn't go over http. You basically directly call the PHP runtime and get the PHP data structure back.
If you want to request data in a PHP script that is on the same server as Piwik, you can use this simple technique.
If you are developing a plugin, you have to use this technique.
<?php define('PIWIK_INCLUDE_PATH', '..'); define('ENABLE_DISPATCH', false); require_once PIWIK_INCLUDE_PATH . "/index.php"; require_once PIWIK_INCLUDE_PATH . "/modules/API/Request.php"; Piwik_FrontController::getInstance()->init(); // We call the API from a php code // it will check that you have the necessary rights // - either you are loggued in piwik and have a cookie in your browser // - or you will have to add the token_auth=XXX to the request string to authenticate // beware that the token_auth changes every time you change your password $request = new Piwik_API_Request(' method=UserSettings.getResolution &idSite=1 &date=yesterday &period=week &format=XML &filter_limit=3 '); $result = $request->process(); echo $result; ?>
Here is the output of this script:
<?xml version="1.0" encoding="utf-8" ?>
<result>
<row>
<label>1280x1024</label>
<nb_uniq_visitors>1169</nb_uniq_visitors>
<nb_visits>1402</nb_visits>
<nb_actions>4792</nb_actions>
<max_actions>202</max_actions>
<sum_visit_length>345788</sum_visit_length>
<bounce_count>640</bounce_count>
</row>
<row>
<label>1024x768</label>
<nb_uniq_visitors>720</nb_uniq_visitors>
<nb_visits>845</nb_visits>
<nb_actions>2379</nb_actions>
<max_actions>170</max_actions>
<sum_visit_length>146387</sum_visit_length>
<bounce_count>424</bounce_count>
</row>
<row>
<label>1280x800</label>
<nb_uniq_visitors>667</nb_uniq_visitors>
<nb_visits>781</nb_visits>
<nb_actions>2426</nb_actions>
<max_actions>159</max_actions>
<sum_visit_length>164020</sum_visit_length>
<bounce_count>364</bounce_count>
</row>
</result>
See also
Find other documentation on Piwik web analytics API
