Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP proxy support #7

Open
sharklasersninja opened this issue Nov 19, 2013 · 0 comments
Open

HTTP proxy support #7

sharklasersninja opened this issue Nov 19, 2013 · 0 comments

Comments

@sharklasersninja
Copy link

The attached patch adds simple http proxy support.

diff --git a/php-ga/src/GoogleAnalytics/Config.php b/php-ga/src/GoogleAnalytics/Config.php
index 7f19142..c0b51f0 100644
--- a/php-ga/src/GoogleAnalytics/Config.php
+++ b/php-ga/src/GoogleAnalytics/Config.php
@@ -113,6 +113,14 @@ class Config {
         * @var string
         */
        protected $endPointHost = 'www.google-analytics.com';
+
+       /**
+        * Connect to $endPointHost through this proxy
+        *
+        * @see Internals\Request\HttpRequest::send()
+        * @var string
+        */
+       protected $httpProxy = null;

        /**
         * Google Analytics tracking request endpoint path
@@ -290,6 +298,21 @@ class Config {
                $this->sitespeedSampleRate = (int)$sitespeedSampleRate;
        }

+       /**
+        * @return string
+        */
+       public function getHttpProxy() {
+               return $this->httpProxy;
+       }
+
+       /**
+        * @param string $host
+        * @param int $port
+        */
+       public function setHttpProxy($host, $port) {
+               $this->httpProxy = sprintf('%s:%d', $host, $port);
+       }
+
 }

 ?>
\ No newline at end of file
diff --git a/php-ga/src/GoogleAnalytics/Internals/Request/HttpRequest.php b/php-ga/src/GoogleAnalytics/Internals/Request/HttpRequest.php
index 2ba81ae..60bf47a 100644
--- a/php-ga/src/GoogleAnalytics/Internals/Request/HttpRequest.php
+++ b/php-ga/src/GoogleAnalytics/Internals/Request/HttpRequest.php
@@ -121,10 +121,10 @@ abstract class HttpRequest {
                $usePost = strlen($queryString) > 2036;

                if(!$usePost) {
-                       $r = 'GET ' . $this->config->getEndpointPath() . '?' . $queryString . ' HTTP/1.0' . "\r\n";
+                       $r = 'GET ' . "http://" . $this->config->getEndpointHost() . $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...
-                       $r = 'POST /p' . $this->config->getEndpointPath() . ' HTTP/1.0' . "\r\n";
+                       $r = 'POST ' . "http://" . $this->config->getEndpointHost() . "/p" . $this->config->getEndpointPath() . ' HTTP/1.0' . "\r\n";
                }
                $r .= 'Host: ' . $this->config->getEndpointHost() . "\r\n";

@@ -180,7 +180,14 @@ abstract class HttpRequest {
                if($this->config->getEndpointHost() !== null) {
                        $timeout = $this->config->getRequestTimeout();

-                       $socket = fsockopen($this->config->getEndpointHost(), 80, $errno, $errstr, $timeout);
+                       $proxy = $this->config->getHttpProxy();
+                       if ($proxy) {
+                               list($host, $port) = explode(':', $proxy);
+                       } else  {
+                               $host = $this->config->getEndpointHost();
+                               $port = 80;
+                       }
+                       $socket = fsockopen($host, $port, $errno, $errstr, $timeout);
                        if(!$socket) return false;

                        if($this->config->getFireAndForget()) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant