Piwik\API\
Dispatches API requests to the appropriate API method.
The Request class is used throughout Matomo (formerly Piwik) to call API methods. The difference between using Request and calling API methods directly is that Request will do more after calling the API including: applying generic filters, applying queued filters, and handling the flat and label query parameters.
Additionally, the Request class will forward current query parameters to the request which is more convenient than calling Common::getRequestVar() many times over.
In most cases, using a Request object to query the API is the correct approach.
The return value of API methods undergo some extra processing before being returned by Request.
The value returned by Request will be serialized to a certain format before being returned.
Basic Usage
$request = new Request('method=UserLanguage.getLanguage&idSite=1&date=yesterday&period=week'
. '&format=xml&filter_limit=5&filter_offset=0')
$result = $request->process();
echo $result;
Getting a unrendered DataTable
// use the convenience method 'processRequest'
$dataTable = Request::processRequest('UserLanguage.getLanguage', array(
'idSite' => 1,
'date' => 'yesterday',
'period' => 'week',
'filter_limit' => 5,
'filter_offset' => 0
'format' => 'original', // this is the important bit
));
echo "This DataTable has " . $dataTable->getRowsCount() . " rows.";
The class defines the following methods:
getRequestArrayFromString()
— Converts the supplied request string into an array of query parameter name/value mappings.__construct()
— Constructor.process()
— Dispatches the API request to the appropriate API method and returns the result after post-processing.getClassNameAPI()
— Returns the name of a plugin's API class by plugin name.isRootRequestApiRequest()
— Detect if the root request (the actual request) is an API request or not.isCurrentApiRequestTheRootApiRequest()
— Checks if the currently executing API request is the root API request or not.isApiRequest()
— Detect if request is an API request.getMethodIfApiRequest()
— Returns the current API method being executed, if the current request is an API request.processRequest()
— Helper method that processes an API request in one line using the variables in $_GET
and $_POST
.getRequestParametersGET()
— Returns the original request parameters in the current query string as an array mapping query parameter names with values.getBaseReportUrl()
— Returns the URL for the current requested report w/o any filter parameters.getCurrentUrlWithoutGenericFilters()
— Returns the current URL without generic filter query parameters.shouldLoadFlatten()
getRawSegmentFromRequest()
— Returns the segment query parameter from the original request, without modifications.getRequestArrayFromString()
Converts the supplied request string into an array of query parameter name/value
mappings. The current query parameters (everything in $_GET
and $_POST
) are
forwarded to request array before it is returned.
$request
(string
|array
|null
) —
The base request string or array, eg, 'module=UserLanguage&action=getLanguage'
.$defaultRequest
(array
) —
Default query parameters. If a query parameter is absent in $request
, it will be loaded from this. Defaults to $_GET + $_POST
.array
value.__construct()
Constructor.
$request
(string
|array
) —
Query string that defines the API call (must at least contain a method parameter), eg, 'method=UserLanguage.getLanguage&idSite=1&date=yesterday&period=week&format=xml'
If a request is not provided, then we use the values in the $_GET
and $_POST
superglobals.$defaultRequest
(array
) —
Default query parameters. If a query parameter is absent in $request
, it will be loaded from this. Defaults to $_GET + $_POST
.process()
Dispatches the API request to the appropriate API method and returns the result after post-processing.
Post-processing includes:
If 'original'
is supplied for the output format, the result is returned as a PHP
object.
DataTable
|Piwik\API\Map
|string
—
The data resulting from the API call.Piwik\Exception\PluginDeactivatedException
— if the module plugin is not activated.Exception
— if the requested API method cannot be called, if required parameters for the
API method are missing or if the API method throws an exception and the format
query parameter is original.getClassNameAPI()
Returns the name of a plugin's API class by plugin name.
It accepts the following parameter(s):
$plugin
(string
) —
The plugin name, eg, 'Referrers'
.Returns: string
—
The fully qualified API class name, eg, '\Piwik\Plugins\Referrers\API'
.
isRootRequestApiRequest()
Detect if the root request (the actual request) is an API request or not. To detect whether an API is currently request within any request, have a look at isApiRequest().
bool
value.isCurrentApiRequestTheRootApiRequest()
Checks if the currently executing API request is the root API request or not.
Note: the "root" API request is the first request made. Within that request, further API methods can be called programmatically. These requests are considered "child" API requests.
bool
value.isApiRequest()
Detect if request is an API request. Meaning the module is 'API' and an API method having a valid format was specified. Note that this method will return true even if the actual request is for example a regular UI reporting page request but within this request we are currently processing an API request (eg a controller calls Request::processRequest('API.getMatomoVersion')). To find out if the root request is an API request or not, call isRootRequestApiRequest()
$request
(array
) —
eg array('module' => 'API', 'method' => 'Test.getMethod')bool
value.getMethodIfApiRequest()
Returns the current API method being executed, if the current request is an API request.
It accepts the following parameter(s):
$request
(array
) —
eg array('module' => 'API', 'method' => 'Test.getMethod')Returns: string
|null
—
It throws one of the following exceptions:
processRequest()
Helper method that processes an API request in one line using the variables in $_GET
and $_POST
.
It accepts the following parameter(s):
$method
(string
) —
The API method to call, ie, 'Actions.getPageTitles'
.$paramOverride
(array
) —
The parameter name-value pairs to use instead of what's in $_GET
& $_POST
.$defaultRequest
(array
) —
Default query parameters. If a query parameter is absent in $request
, it will be loaded from this. Defaults to $_GET + $_POST
. To avoid using any parameters from $_GET or $_POST, set this to an empty array()
.Returns: mixed
—
The result of the API request. See process().
getRequestParametersGET()
Returns the original request parameters in the current query string as an array mapping
query parameter names with values. The result of this function will not be affected
by any modifications to $_GET
and will not include parameters in $_POST
.
array
value.getBaseReportUrl()
Returns the URL for the current requested report w/o any filter parameters.
$module
(string
) —
The API module.$action
(string
) —
The API action.$queryParams
(array
) —
Query parameter overrides.string
value.getCurrentUrlWithoutGenericFilters()
Returns the current URL without generic filter query parameters.
$params
(array
) —
Query parameter values to override in the new URL.string
value.shouldLoadFlatten()
bool
value.getRawSegmentFromRequest()
Returns the segment query parameter from the original request, without modifications.
array
|bool
—