PHP Class Piwik\API\Request

The Request class is used throughout 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 {@link Piwik\Common::getRequestVar()} many times over. In most cases, using a Request object to query the API is the correct approach. ### Post-processing The return value of API methods undergo some extra processing before being returned by Request. ### Output Formats The value returned by Request will be serialized to a certain format before being returned. ### Examples **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.";
See also: http://piwik.org/docs/analytics-api
Show file Open project: piwik/piwik Class Usage Examples

Public Methods

Method Description
__construct ( string | array $request = null, array $defaultRequest = null ) Constructor.
getBaseReportUrl ( string $module, string $action, array $queryParams = [] ) : string Returns the URL for the current requested report w/o any filter parameters.
getClassNameAPI ( string $plugin ) : string Returns the name of a plugin's API class by plugin name.
getCurrentUrlWithoutGenericFilters ( array $params ) : string Returns the current URL without generic filter query parameters.
getRawSegmentFromRequest ( ) : array | boolean Returns the segment query parameter from the original request, without modifications.
getRenamedModuleAndAction ( $module, $action ) : array( For backward compatibility: Piwik API still works if module=Referers, we rewrite to correct renamed plugin: Referrers
getRequestArrayFromString ( string | array | null $request, array $defaultRequest = null ) : array Converts the supplied request string into an array of query paramater name/value mappings. The current query parameters (everything in $_GET and $_POST) are forwarded to request array before it is returned.
getRequestParametersGET ( ) : array 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.
isApiRequest ( array $request ) : boolean Detect if request is an API request. Meaning the module is 'API' and an API method having a valid format was specified.
process ( ) : DataTable | Map | string Dispatches the API request to the appropriate API method and returns the result after post-processing.
processRequest ( string $method, array $paramOverride = [], array $defaultRequest = null ) : mixed Helper method that processes an API request in one line using the variables in $_GET and $_POST.
reloadAuthUsingTokenAuth ( array $request = null ) : void If the token_auth is found in the $request parameter, the current session will be authenticated using this token_auth.
shouldLoadExpanded ( ) : boolean Returns whether the DataTable result will have to be expanded for the current request before rendering.
shouldLoadFlatten ( ) : boolean

Private Methods

Method Description
extractModuleAndMethod ( string $parameter ) : array Returns array($class, $method) from the given string $class.$method
forceReloadAuthUsingTokenAuth ( string $tokenAuth ) : void The current session will be authenticated using this token_auth.
getDefaultRequest ( ) : array
renameModuleAndActionInRequest ( )
restoreAuthUsingTokenAuth ( $tokenToRestore, $hadSuperUserAccess )
sanitizeRequest ( ) Make sure that the request contains no logical errors
shouldReloadAuthUsingTokenAuth ( $request )

Method Details

__construct() public method

Constructor.
public __construct ( string | array $request = null, array $defaultRequest = null )
$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`.

getBaseReportUrl() public static method

Returns the URL for the current requested report w/o any filter parameters.
public static getBaseReportUrl ( string $module, string $action, array $queryParams = [] ) : string
$module string The API module.
$action string The API action.
$queryParams array Query parameter overrides.
return string

getClassNameAPI() public static method

Returns the name of a plugin's API class by plugin name.
public static getClassNameAPI ( string $plugin ) : string
$plugin string The plugin name, eg, `'Referrers'`.
return string The fully qualified API class name, eg, `'\Piwik\Plugins\Referrers\API'`.

getCurrentUrlWithoutGenericFilters() public static method

Returns the current URL without generic filter query parameters.
public static getCurrentUrlWithoutGenericFilters ( array $params ) : string
$params array Query parameter values to override in the new URL.
return string

getRawSegmentFromRequest() public static method

Returns the segment query parameter from the original request, without modifications.
public static getRawSegmentFromRequest ( ) : array | boolean
return array | boolean

getRenamedModuleAndAction() public static method

For backward compatibility: Piwik API still works if module=Referers, we rewrite to correct renamed plugin: Referrers
public static getRenamedModuleAndAction ( $module, $action ) : array(
$module
$action
return array(

getRequestArrayFromString() public static method

Converts the supplied request string into an array of query paramater name/value mappings. The current query parameters (everything in $_GET and $_POST) are forwarded to request array before it is returned.
public static getRequestArrayFromString ( string | array | null $request, array $defaultRequest = null ) : array
$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`.
return array

getRequestParametersGET() public static method

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.
public static getRequestParametersGET ( ) : array
return array

isApiRequest() public static method

Detect if request is an API request. Meaning the module is 'API' and an API method having a valid format was specified.
public static isApiRequest ( array $request ) : boolean
$request array eg array('module' => 'API', 'method' => 'Test.getMethod')
return boolean

process() public method

Post-processing includes: - flattening if **flat** is 0 - running generic filters unless **disable_generic_filters** is set to 1 - URL decoding label column values - running queued filters unless **disable_queued_filters** is set to 1 - removing columns based on the values of the **hideColumns** and **showColumns** query parameters - filtering rows if the **label** query parameter is set - converting the result to the appropriate format (ie, XML, JSON, etc.) If 'original' is supplied for the output format, the result is returned as a PHP object.
public process ( ) : DataTable | Map | string
return Piwik\DataTable | Map | string The data resulting from the API call.

processRequest() public static method

Helper method that processes an API request in one line using the variables in $_GET and $_POST.
public static processRequest ( string $method, array $paramOverride = [], array $defaultRequest = null ) : mixed
$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()`.
return mixed The result of the API request. See {@link process()}.

reloadAuthUsingTokenAuth() public static method

It will overwrite the previous Auth object.
public static reloadAuthUsingTokenAuth ( array $request = null ) : void
$request array If null, uses the default request ($_GET)
return void

shouldLoadExpanded() public static method

Returns whether the DataTable result will have to be expanded for the current request before rendering.
public static shouldLoadExpanded ( ) : boolean
return boolean

shouldLoadFlatten() public static method

public static shouldLoadFlatten ( ) : boolean
return boolean