Example #1
0
 /**
  * This method should only be called directly or indirectly by fire(), but must
  * remain public as it can be called by a closure function.
  *
  * Sends either a normal HTTP request with response or an asynchronous request
  * to Google Analytics without waiting for the response. Will always return
  * null in the latter case, or false if any connection problems arise.
  *
  * @see HttpRequest::fire()
  * @param string $request
  * @return null|string|bool
  */
 public function _send()
 {
     $request = $this->buildHttpRequest();
     $response = null;
     // Do not actually send the request if endpoint host is set to null
     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->getFireAndForget()) {
             stream_set_blocking($socket, false);
         }
         $timeoutS = intval($timeout);
         $timeoutUs = ($timeout - $timeoutS) * 100000;
         stream_set_timeout($socket, $timeoutS, $timeoutUs);
         // Ensure that the full request is sent (see http://code.google.com/p/php-ga/issues/detail?id=11)
         $sentData = 0;
         $toBeSentData = strlen($request);
         while ($sentData < $toBeSentData) {
             $sentData += fwrite($socket, $request);
         }
         if (!$this->config->getFireAndForget()) {
             while (!feof($socket)) {
                 $response .= fgets($socket, 512);
             }
         }
         fclose($socket);
     }
     if ($loggingCallback = $this->config->getLoggingCallback()) {
         $loggingCallback($request, $response);
     }
     return $response;
 }