Example #1
0
 /**
  * @return string
  */
 protected function buildHttpRequest()
 {
     $parameters = $this->buildParameters();
     // This constant is supported as the 4th argument of http_build_query()
     // from PHP 5.3.6 on and will tell it to use rawurlencode() instead of urlencode()
     // internally, see http://code.google.com/p/php-ga/issues/detail?id=3
     if (defined('PHP_QUERY_RFC3986')) {
         // http_build_query() does automatically skip all array entries
         // with null values, exactly what we want here
         $queryString = http_build_query($parameters->toArray(), '', '&', PHP_QUERY_RFC3986);
     } else {
         // Manually replace "+"s with "%20" for backwards-compatibility
         $queryString = str_replace('+', '%20', http_build_query($parameters->toArray(), '', '&'));
     }
     // Mimic Javascript's encodeURIComponent() encoding for the query
     // string just to be sure we are 100% consistent with GA's Javascript client
     $queryString = Util::convertToUriComponentEncoding($queryString);
     // Recent versions of ga.js use HTTP POST requests if the query string is too long
     $usePost = strlen($queryString) > 2036;
     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...
         $r = 'POST /p' . $this->config->getEndpointPath() . ' HTTP/1.0' . "\r\n";
     }
     $r .= 'Host: ' . $this->config->getEndpointHost() . "\r\n";
     if ($this->userAgent) {
         $r .= 'User-Agent: ' . str_replace(array("\n", "\r"), '', $this->userAgent) . "\r\n";
     }
     if ($this->xForwardedFor) {
         // Sadly "X-Fowarded-For" is not supported by GA so far,
         // see e.g. http://www.google.com/support/forum/p/Google+Analytics/thread?tid=017691c9e71d4b24,
         // but we include it nonetheless for the pure sake of correctness (and hope)
         $r .= 'X-Forwarded-For: ' . str_replace(array("\n", "\r"), '', $this->xForwardedFor) . "\r\n";
     }
     if ($usePost) {
         // Don't ask me why "text/plain", but ga.js says so :)
         $r .= 'Content-Type: text/plain' . "\r\n";
         $r .= 'Content-Length: ' . strlen($queryString) . "\r\n";
     }
     $r .= 'Connection: close' . "\r\n";
     $r .= "\r\n\r\n";
     if ($usePost) {
         $r .= $queryString;
     }
     return $r;
 }