Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions MatomoTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,21 @@ public function setRequestTimeout($timeout)
$this->requestTimeout = $timeout;
return $this;
}

/**
* Sets the request method to POST, which is recommended when using setTokenAuth()
* to prevent the token from being recorded in server logs. Avoid using redirects
* when using POST to prevent the loss of POST values. When using Log Analytics,
* be aware that POST requests are not parseable/replayable.
*
* @param string $method Either 'POST' or 'GET'
* @return $this
*/
public function setRequestMethodNonBulk($method)
{
$this->requestMethod = strtoupper($method) === 'POST' ? 'POST' : 'GET';
return $this;
}

/**
* If a proxy is needed to look up the address of the Matomo site, set it with this
Expand Down Expand Up @@ -1533,6 +1548,18 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal

$proxy = $this->getProxy();

if (isset($this->requestMethod)
&& $this->requestMethod === 'POST'
&& !$this->doBulkRequests
) {
$urlParts = explode('?', $url);

$url = $urlParts[0];
$postData = $urlParts[1];

$method = 'POST';
}

if (function_exists('curl_init') && function_exists('curl_exec')) {
$options = array(
CURLOPT_URL => $url,
Expand Down Expand Up @@ -1564,6 +1591,11 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
default:
break;
}

if (isset($postData)) {
$options[CURLOPT_HTTPHEADER][] = 'Content-Type: application/x-www-form-urlencoded';
$options[CURLOPT_POSTFIELDS] = $postData;
}

// only supports JSON data
if (!empty($data)) {
Expand Down Expand Up @@ -1603,6 +1635,11 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal
if (isset($proxy)) {
$stream_options['http']['proxy'] = $proxy;
}

if (isset($postData)) {
$stream_options['http']['header'] .= 'Content-Type: application/x-www-form-urlencoded';
$stream_options['http']['content'] = $postData;
}

// only supports JSON data
if (!empty($data)) {
Expand Down