Download a Matomo Widget with cURL

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

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.

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

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.

1 Like

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

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.

Hi, Lukas!

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

Roddy