Jump to content
iwato

A Matomo Bulk HTTP Request

Recommended Posts

REQUEST:  Please compare the following PHP code with the intended result, and if possible, say why only the last urls[ ] parameter is being read.

BACKGROUND:  The following code is part of a successfully received and partially processed HTTP POST Bulk Request to the Matomo application.  Both of the called methods -- namely,  Events.getCategory and Events.getAction -- have been tested separately and are perfectly functional.  Further, the Matomo application reports no dysfunction, and there are no warning, notices or other indicated sources of foul coding in the PHP log file.

$result = '';
$query_str = '';
$i = 0;
$token_auth = '...';			
function set_url_request($method, $params) {					
    $request = "method=$method";
    foreach ($params as $param) {
        $request .= "&" . $param;
    }
    return rawurlencode($request);
}
$url = "https:/.../index.php";
$url .= "?module=API";
$url .= "&method=API.getBulkRequest";
$url .= "&format=json";
$url .= "&token_auth=$token_auth";
foreach ($_POST['methodName'] as $method) {
    $method = filter_var($method, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    if ($method == '_getCategory') {
        $method = "Events." . substr($method, 1);			
        $params = array("idSite=1","period=year","date=today","format=json");
        $query_str = set_url_request($method, $params);
        $url .= "&urls[$i++]=" . $query_str;
    }				
    if ($method == '_getAction') {
        $method = "Events." . substr($method, 1);			
        $params = array("idSite=1","period=year","date=today","format=json");
        $query_str = set_url_request($method, $params);
        $url .= "&urls[$i++]=" . $query_str;
    }				
}				

The above is suppose to yield code formatted in a manner similar to the following HTTP request.  Obviously, I have modified it in important ways,  Important, however, is that the &urls[0] request appears to be ignored.

https://demo.piwik.org/index.php
?module=API
&method=API.getBulkRequest
&format=json
&urls[0]=
	method%3dVisitsSummary.get
	%26idSite%3d3
	%26date%3d2012-03-06
	%26period%3dday
&urls[1]=
	method%3dVisitorInterest.getNumberOfVisitsPerVisitDuration
	%26idSite%3d3
	%26date%3d2012-03-06
	%26period%3dday

Roddy

Share this post


Link to post
Share on other sites

Print $url to see what you're actually setting it to, it does not look like your example output.  You should probably also encode those square brackets, they are defined as reserved characters which may be used as delimiters.

  • Thanks 1

Share this post


Link to post
Share on other sites

Hooray!  Hooray!  I can now make Matomo bulk requests.
I fixed the problem by separating out the index variable ($i).

$url .= "&urls[" . $i++ . "]=" . $query_str;

Roddy

Share this post


Link to post
Share on other sites

Right.  You can only substitute variables in strings, PHP will not recognize an expression inside a string.

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×