piwik api: 调用api数据
原创 2014年08月23日 17:17:48
1.关于api
2.api的使用
参考网址:http://developer.piwik.org/guides/querying-the-reporting-api
- <?php
- use Piwik\API\Request;
- use Piwik\FrontController;
- define('PIWIK_INCLUDE_PATH', realpath('../..'));
- define('PIWIK_USER_PATH', realpath('../..'));
- define('PIWIK_ENABLE_DISPATCH', false);
- define('PIWIK_ENABLE_ERROR_HANDLER', false);
- define('PIWIK_ENABLE_SESSION_START', false);
- // if you prefer not to include 'index.php', you must also define here PIWIK_DOCUMENT_ROOT
- // and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
- require_once PIWIK_INCLUDE_PATH . "/index.php";
- require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
- FrontController::getInstance()->init();
- // This inits the API Request with the specified parameters
- $request = new Request('
- module=API
- &method=UserSettings.getResolution
- &idSite=7
- &date=yesterday
- &period=week
- &format=XML
- &filter_limit=3
- &token_auth=anonymous
- ');
- // Calls the API and fetch XML data back
- $result = $request->process();
- echo $result;
- Here is the output of this script:
- <?xml version="1.0" encoding="utf-8" ?>
- <result>
- <row>
- <label>1920x1080</label>
- <nb_visits>1742</nb_visits>
- <nb_actions>4343</nb_actions>
- <max_actions>89</max_actions>
- <sum_visit_length>349083</sum_visit_length>
- <bounce_count>1135</bounce_count>
- <nb_visits_converted>52</nb_visits_converted>
- <sum_daily_nb_uniq_visitors>1503</sum_daily_nb_uniq_visitors>
- </row>
- <row>
- <label>1366x768</label>
- <nb_visits>1045</nb_visits>
- <nb_actions>2214</nb_actions>
- <max_actions>38</max_actions>
- <sum_visit_length>147636</sum_visit_length>
- <bounce_count>747</bounce_count>
- <nb_visits_converted>26</nb_visits_converted>
- <sum_daily_nb_uniq_visitors>937</sum_daily_nb_uniq_visitors>
- </row>
- <row>
- <label>1680x1050</label>
- <nb_visits>533</nb_visits>
- <nb_actions>1358</nb_actions>
- <max_actions>47</max_actions>
- <sum_visit_length>117723</sum_visit_length>
- <bounce_count>345</bounce_count>
- <nb_visits_converted>11</nb_visits_converted>
- <sum_daily_nb_uniq_visitors>459</sum_daily_nb_uniq_visitors>
- </row>
- </result>
api调用的方式:
- <?php
- exit; // REMOVE this line to run the script
- // 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=7
- $url = "http://demo.piwik.org/";
- $url .= "?module=API&method=Referrers.getKeywords";
- $url .= "&idSite=7&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 = htmlspecialchars(html_entity_decode(urldecode($row['label']), ENT_QUOTES), ENT_QUOTES);
- $hits = $row['nb_visits'];
- print("<b>$keyword</b> ($hits hits)<br>");
- }
- Here is the output of this code:
- <h1>Keywords for the last month</h1><b>Keyword not defined</b> (16481 hits)<br><b>xxxxxx</b> (80 hits)<br><b>xxx.xxx</b> (9 hits)<br><b>downloads anzeigen</b> (7 hits)<br><b>youtube-downloader.savetubevideo.com / referral</b> (4 hits)<br><b>meine downloads anzeigen</b> (3 hits)<br><b>understanding piwik</b> (3 hits)<br><b>bbw highway .com</b> (2 hits)<br><b>imgsrc</b> (2 hits)<br><b>piwik</b> (2 hits)<br><b>piwik deutsch</b> (2 hits)<br><b>piwik exporting reports</b> (2 hits)<br><b>piwik failed to load html file</b> (2 hits)<br><b>piwik not tracking</b> (2 hits)<br><b>piwik sharepoint</b> (2 hits)<br><b>premature end of data</b> (2 hits)<br><b>vertrag mit piwik zur auftragsdatenverarbeitung</b> (2 hits)<br><b>wp-piwik einrichten</b> (2 hits)<br><b>"edt 2014 x86_64" ext:php</b> (1 hits)<br><b>"failed to load html file" piwik plugins/sitesmanager/templates/index.html</b> (1 hits)<br