@@ -32,10 +32,12 @@ public static function allowed() {
3232 /* Matomo API calls
3333 ---------------------------------*/
3434
35- protected $ url = null ;
36- protected $ id = null ;
37- protected $ token = null ;
35+ protected $ url = null ;
36+ protected $ id = null ;
37+ protected $ token = null ;
38+ protected $ client = null ;
3839 protected $ requestParams = [];
40+
3941 protected $ methodsMap = array (
4042 'referrerType ' => 'Referrers.getReferrerType ' ,
4143 'websites ' => 'Referrers.getWebsites ' ,
@@ -46,9 +48,12 @@ public static function allowed() {
4648 );
4749
4850 public function __construct () {
49- $ this ->url = option ('sylvainjule.matomo.url ' );
50- $ this ->id = option ('sylvainjule.matomo.id ' );
51- $ this ->token = is_callable ($ this ->token ) ? $ this ->token () : option ('sylvainjule.matomo.token ' );
51+ $ this ->url = option ('sylvainjule.matomo.url ' );
52+ $ this ->id = option ('sylvainjule.matomo.id ' );
53+ $ this ->token = is_callable ($ this ->token ) ? $ this ->token () : option ('sylvainjule.matomo.token ' );
54+ $ this ->client = new GuzzleHttp \Client ();
55+
56+ $ this ->requestParams ['token_auth ' ] = $ this ->token ;
5257 if (option ('sylvainjule.matomo.basicAuth ' ) !== null ) {
5358 $ this ->requestParams ['basicAuth ' ] = option ('sylvainjule.matomo.basicAuth ' );
5459 }
@@ -60,9 +65,8 @@ public function apiWidget($widget, $method, $period, $date, $limit, $lang) {
6065 $ url .= "&idSite= " . $ this ->id ."&period= " . $ period ."&date= " . $ date ;
6166 $ url .= "&format=JSON&language= " . $ lang ;
6267 $ url .= $ limit ? "&filter_limit= " . $ limit : '' ;
63- $ url .= "&token_auth= " . $ this ->token ;
6468
65- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
69+ $ content = $ this ->sendRequest ( $ url );
6670 return $ content ;
6771 }
6872
@@ -72,7 +76,7 @@ public function apiBulkWidgets($widgets, $period, $date, $limit, $lang) {
7276 $ url = $ this ->url ;
7377
7478 $ url .= "?module=API&method=API.getBulkRequest " ;
75- $ url .= "&token_auth= " . $ this -> token . " & format=JSON " ;
79+ $ url .= "&format=JSON " ;
7680
7781 $ i = 0 ;
7882 foreach ($ widgets as $ widget ) {
@@ -81,27 +85,27 @@ public function apiBulkWidgets($widgets, $period, $date, $limit, $lang) {
8185 $ i ++;
8286 }
8387
84- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
88+ $ content = $ this ->sendRequest ( $ url );
8589 return $ content ;
8690 }
8791
8892 public function apiChart ($ method , $ period , $ date ) {
8993 $ url = $ this ->url ;
9094 $ url .= "?module=API&method= " . $ method ;
9195 $ url .= "&idSite= " . $ this ->id ."&period= " . $ period ."&date= " . $ date ;
92- $ url .= "&format=JSON&token_auth= " . $ this -> token ;
96+ $ url .= "&format=JSON " ;
9397
94- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
98+ $ content = $ this ->sendRequest ( $ url );
9599 return $ content ;
96100 }
97101
98102 public function apiOverview ($ method , $ period , $ date ) {
99103 $ url = $ this ->url ;
100104 $ url .= "?module=API&method= " . $ method ;
101105 $ url .= "&idSite= " . $ this ->id ."&period= " . $ period ."&date= " . $ date ;
102- $ url .= "&format=JSON&token_auth= " . $ this -> token ;
106+ $ url .= "&format=JSON " ;
103107
104- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
108+ $ content = $ this ->sendRequest ( $ url );
105109 return $ content ;
106110 }
107111
@@ -111,17 +115,17 @@ public function apiRealtime() {
111115
112116 $ url .= "?module=API&method= " . $ method ;
113117 $ url .= "&idSite= " . $ this ->id ."&lastMinutes=3 " ;
114- $ url .= "&format=JSON&token_auth= " . $ this -> token ;
118+ $ url .= "&format=JSON " ;
115119
116- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
120+ $ content = $ this ->sendRequest ( $ url );
117121 return $ content ;
118122 }
119123
120124 public function apiBulkSummary () {
121125 $ url = $ this ->url ;
122126
123127 $ url .= "?module=API&method=API.getBulkRequest " ;
124- $ url .= "&token_auth= " . $ this -> token . " & format=JSON " ;
128+ $ url .= "&format=JSON " ;
125129
126130 $ url .= "&urls[0]= " ;
127131 $ url .= urlencode ("method=VisitsSummary.getVisits&idSite= " . $ this ->id ."&period=day&date=today " );
@@ -135,20 +139,21 @@ public function apiBulkSummary() {
135139 $ url .= "&urls[3]= " ;
136140 $ url .= urlencode ("method=VisitsSummary.getVisits&idSite= " . $ this ->id ."&period=year&date=today " );
137141
138- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
142+ $ content = $ this ->sendRequest ( $ url );
139143 return $ content ;
140144 }
141145
142146 public function apiPageMetrics ($ period , $ uri , $ lang ) {
143147 $ url = $ this ->url ;
144148 $ url .= "?module=API&method=Actions.getPageUrls " ;
145149 $ url .= "&idSite= " . $ this ->id ."&period= " . $ period ."&date=today " ;
146- $ url .= "&format=JSON&token_auth= " . $ this -> token ;
150+ $ url .= "&format=JSON " ;
147151 $ url .= '&flat=1 ' ;
148152 $ url .= $ lang ['multilang ' ] ? '&expanded=1 ' : '' ;
149153
150- $ content = Remote:: get ( $ url , $ this ->requestParams )-> json ( );
154+ $ content = $ this ->sendRequest ( $ url );
151155 $ content = $ this ->filterPageMetrics ($ content , $ uri , $ lang );
156+
152157 return $ content ;
153158 }
154159
@@ -300,4 +305,10 @@ public function multiplyArraysByKey($arr1, $arr2) {
300305 return $ total ;
301306 }
302307
308+ private function sendRequest ($ url ) {
309+ $ response = $ this ->client ->request ('POST ' , $ url , ['form_params ' => $ this ->requestParams ]);
310+ $ response = json_decode ($ response ->getBody ()->getContents (), true );
311+ return $ response ;
312+ }
313+
303314}
0 commit comments