Querying and Parsing with the Matomo REST API

QUESTION: What is the Matomo class that determines how the file retrieved, when the file_get_contents() function is called, is filled?

Now, I understand that there are many subordinate classes/modules that likely inherit the sought after class. It is my goal, however, to learn how to make customized HTTP REST requests and manipulate their content. To this end I must know how the retrieved file is configured, so that I can properly parse its content.

Ultimately I want to produce my own widgets on my own website via AJAX, for in this way I can access the Matomo server directly, process the result in an invisible PHP file that does not reveal its source, retrieve the output with AJAX, and then format it for display on my website.

I know, it is like inventing the Matomo wheel all over again, but I do not know enough about Matomo and likely even PHP to fix the security issue ot the Matomo widgets.

Roddy

Just use cURL and you can very easy foretell the response. The API can (as far as I know) return XML, JSON, Orignal (and some more).

Hi Fabian!

Thank you for responding.

After some investigation about the use of cURL I realized that I can probably achieve my goal directly with PHP. My reason for this realization is that my Matomo server applicatiion and website are installed on the same host server. Does this make sense?

Roddy

I’m not quite sure what you mean. But I thought about this: PHP: cURL - Manual

Yes, going to the PHP manual is what caused me to post my last entry. It was the horrible thought that I would have to become an expert in cURL in order to get Matomo to work for me in the matter that I had hoped.

According to my host server I can use PHP across domains residing on the same server. This suggests that I can call the Matomo server directly from my website without having to make an HTTP request.

My Matomo server resides in a domain called nudge.online, and my website resides in a different domain called grammarcaptive.com. These domains, however, simply represent different names for two different folders that reside in the same parent folder called public_html. Because I can use the same PHP module for both folders I do not need access to cURL to achieve my goals. Does this make sense to you now?

Roddy

Hi,

So if I understand you correctly, you want to access the API of your local Matomo instance without going over HTTP.

I’m afraid this isn’t possible. The API is intentionally so general that it can be accessed for every use case. And it is an standardisised interface for every access.

But going over HTTP is probably far easier than you imagine (or than going via PHP, as you’d have to load all of Matomos code including dependencies).

Even many parts of Matomo including archiving and some CLI commands make HTTP requests to itself so that only one interface has to be maintained.

So, tracking with PHP and reporting with PHP are two completely different matters. Whereas I can both track and report with HTTP, with PHP I can only track. Is this correct?

Roddy

Yes, but the PHP-tracking-client just makes the correct HTTP-requests to the reporting API.

1 Like

Ah ha!
So, I become an expert in the use of cURL.

Roddy

cURL is really really easy and you do not need to learn all depths of it.

This simple code will make an call to the given URL and puts the result into the variable $output.

<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);     
?>

You can use this example for every single call you want to make, the only thing that matters to you than is this page: PHP: curl_setopt - Manual

This page contains all the many many options you may use to alter the way cURL works.

1 Like

Jawohl, dies meinte ich. Nur 144 Optionen!
Yes, I agree. Only 144 options …

LOL

Roddy