Download a Matomo Widget with cURL


(Roddy A. Stegemann) #1

What is the proper way to download a Matomo widget with cURL? The following code succeeds in obtaining what appears to be the widget’s HTML, but all of the CSS, javascript functionality, and imagery are lost.

    $url = 'https://www.nudge.online ... matomo/index.php?module=Widgetize&action=iframe&widget=1&moduleToWidgetize=Live&actionToWidgetize=widget&idSite=1&period=day&date=today&disableLink=1&token_auth=...';
    $curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 0);
    if(curl_exec($curl_request) === false) {
        echo 'Curl error: ' . curl_error($curl_request);
    } else {
        echo 'Operation completed without any errors';
    }

Roddy


(Peterbo) #2

Of course, CSS, images, JS is lost. You only (can) download the HTML via cUrl. CSS/JS etc. is only applied in the context you load it to (e.g. Matomo GUI).

Everywhere else, you have to apply your own Styles/UX. The only way to keep look&feel of the widgets without hassle is to embed them via iFrame.


(Roddy A. Stegemann) #3

Thank you for responding, Peterbo!

The problem with getting Matomo widgets to download without hassle is the user must expose his authorization token and thereby the entire reporting API. I wish to avoid this.

Do you know the minimum file requirements to get the FrontController class to load properly?

Roddy


(Fabian Dellwing) #4

There is a way around this, but it is a bit uglier than cURL. You could try this code:

exec('wget -P /tmp/ -p -k https://www.nudge.online ... matomo/index.php?module=Widgetize&action=iframe&widget=1&moduleToWidgetize=Live&actionToWidgetize=widget&idSite=1&period=day&date=today&disableLink=1&token_auth=...');

This will download the complete page into an own folder in /tmp. I don’t know if this helps you.


(Roddy A. Stegemann) #5

Hi, Fabian. It appears that it would help, because the widget must be completely formatted before it is read into the src attribute of an iframe. Unfortunately, the wget application is not available on shared SSH servers.

Do you know of another application that can achieve the same?

Roddy


(Lukas Winkler) #6

Curl can also download all files, but it doesn’t have a recursive mode and doesn’t update the URLs so they are still working like wget.


(Roddy A. Stegemann) #7

Hi, Lukas!

The whole idea of using wget is to overcome the short-comings of cURL in this regard.

Roddy