diff --git a/src/GoogleAnalytics/Config.php b/src/GoogleAnalytics/Config.php index 7f19142..494ef68 100644 --- a/src/GoogleAnalytics/Config.php +++ b/src/GoogleAnalytics/Config.php @@ -62,6 +62,14 @@ class Config { */ const ERROR_SEVERITY_EXCEPTIONS = 2; + + /** + * If you need to use a proxy server to make request set to ture + * + * @var bool + */ + protected $useProxy = false; + /** * Whether to just queue all requests on HttpRequest::fire() and actually send * them on PHP script shutdown after all other tasks are done. @@ -290,6 +298,44 @@ public function setSitespeedSampleRate($sitespeedSampleRate) { $this->sitespeedSampleRate = (int)$sitespeedSampleRate; } + + /** + * @param string $ip + * @param int $port + */ + public function setProxy ($ip, $port) + { + $this->proxy = $ip; + $this->port = $port; + $this->useProxy = true; + } + + /** + * @return bool + */ + public function getUseProxy () + { + return $this->useProxy; + } + + + /** + * @return string + */ + public function getProxyIp () + { + return $this->proxy; + } + + /** + * @return int + */ + public function getProxyPort () + { + return $this->port; + } + + } ?> \ No newline at end of file diff --git a/src/GoogleAnalytics/Internals/Request/HttpRequest.php b/src/GoogleAnalytics/Internals/Request/HttpRequest.php index 2ba81ae..4ac123a 100644 --- a/src/GoogleAnalytics/Internals/Request/HttpRequest.php +++ b/src/GoogleAnalytics/Internals/Request/HttpRequest.php @@ -120,7 +120,9 @@ protected function buildHttpRequest() { // Recent versions of ga.js use HTTP POST requests if the query string is too long $usePost = strlen($queryString) > 2036; - if(!$usePost) { + if ($this->config->getUseProxy()) + $r = 'GET http://'. $this->config->getEndpointHost() . $this->config->getEndpointPath() . '?' . $queryString . ' HTTP/1.0' . "\r\n"; + else if(!$usePost) { $r = 'GET ' . $this->config->getEndpointPath() . '?' . $queryString . ' HTTP/1.0' . "\r\n"; } else { // FIXME: The "/p" shouldn't be hardcoded here, instead we need a GET and a POST endpoint... @@ -180,8 +182,12 @@ public function _send() { if($this->config->getEndpointHost() !== null) { $timeout = $this->config->getRequestTimeout(); - $socket = fsockopen($this->config->getEndpointHost(), 80, $errno, $errstr, $timeout); - if(!$socket) return false; + if ($this->config->getUseProxy()) + $socket = fsockopen($this->config->getProxyIp(), $this->config->getProxyPort(), $errno, $errstr, $timeout); + else + $socket = fsockopen($this->config->getEndpointHost(), 80, $errno, $errstr, $timeout); + + if(!$socket) return false; if($this->config->getFireAndForget()) { stream_set_blocking($socket, false);