public function flushBatch($messages) {
    $body = $this->payload($messages);
    $payload = json_encode($body);

    // Escape for shell usage.
    $payload = escapeshellarg($payload);
    $secret = $this->secret;

    $protocol = $this->ssl() ? "https://" : "http://";
    if ($this->host) {
      $host = $this->host;
    } else {
      $host = "api.segment.io";
    }
    $path = "/v1/import";
    $url = $protocol . $host . $path;

    $cmd = "curl -u ${secret}: -X POST -H 'Content-Type: application/json'";
    
    $tmpfname = "";
    if ($this->compress_request) {
      // Compress request to file
      $tmpfname = tempnam("/tmp", "forkcurl_");
      $cmd2 = "echo " . $payload . " | gzip > " . $tmpfname;
      exec($cmd2, $output, $exit);

      if (0 != $exit) {
        $this->handleError($exit, $output);
        return false;
      }

      $cmd.= " -H 'Content-Encoding: gzip'";

      $cmd.= " --data-binary '@" . $tmpfname . "'";
    } else {
      $cmd.= " -d " . $payload;
    }
    
    $cmd.= " '" . $url . "'";

    // Verify message size is below than 32KB
    if (strlen($payload) >= 32 * 1024) {
      if ($this->debug()) {
        $msg = "Message size is larger than 32KB";
        error_log("[Analytics][" . $this->type . "] " . $msg);
      }

      return false;
    }

    // Send user agent in the form of {library_name}/{library_version} as per RFC 7231.
    $library = $messages[0]['context']['library'];
    $libName = $library['name'];
    $libVersion = $library['version'];
    $cmd.= " -H 'User-Agent: ${libName}/${libVersion}'";

    if (!$this->debug()) {
      $cmd .= " > /dev/null 2>&1 &";
    }

    exec($cmd, $output, $exit);

    if (0 != $exit) {
      $this->handleError($exit, $output);
    }

    if ($tmpfname != "") {
      unlink($tmpfname);
    }

    return 0 == $exit;
  }