src/HTTP/Request2.php
public function setHeader($name, $value = null, $replace = true)
    {
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                if (is_string($k)) {
                    $this->setHeader($k, $v, $replace);
                } else {
                    $this->setHeader($v, null, $replace);
                }
            }
        } else {
            if (null === $value && strpos($name, ':')) {
                list($name, $value) = array_map('trim', explode(':', $name, 2));
            }
            // Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2
            if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) {
                throw new HTTP_Request2_LogicException(
                    "Invalid header name '{$name}'",
                    HTTP_Request2_Exception::INVALID_ARGUMENT
                );
            }
            // Header names are case insensitive anyway
            $name = strtolower($name);
            if (null === $value) {
                unset($this->headers[$name]);

            } else {
                if (is_array($value)) {
                    $value = implode(', ', array_map('trim', $value));
                } elseif (is_string($value)) {
                    $value = trim($value);
                }
                if (!isset($this->headers[$name]) || $replace) {
                    $this->headers[$name] = $value;
                } else {
                    $this->headers[$name] .= ', ' . $value;
                }
            }
        }

        return $this;
    }
public function setHeader($name, $value = null, $replace = true)
    {
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                if (is_string($k)) {
                    $this->setHeader($k, $v, $replace);
                } else {
                    $this->setHeader($v, null, $replace);
                }
            }
        } else {
            if (null === $value && strpos($name, ':')) {
                list($name, $value) = array_map('trim', explode(':', $name, 2));
            }
            // Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2
            if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) {
                throw new HTTP_Request2_LogicException(
                    "Invalid header name '{$name}'",
                    HTTP_Request2_Exception::INVALID_ARGUMENT
                );
            }
            // Header names are case insensitive anyway
            $name = strtolower($name);
            if (null === $value) {
                unset($this->headers[$name]);

            } else {
                if (is_array($value)) {
                    $value = implode(', ', array_map('trim', $value));
                } elseif (is_string($value)) {
                    $value = trim($value);
                }
                if (!isset($this->headers[$name]) || $replace) {
                    $this->headers[$name] = $value;
                } else {
                    $this->headers[$name] .= ', ' . $value;
                }
            }
        }

        return $this;
    }
Sets request header(s)

The first parameter may be either a full header string 'header: value' or
header name. In the former case $value parameter is ignored, in the latter
the header's value will either be set to $value or the header will be
removed if $value is null. The first parameter can also be an array of
headers, in that case method will be called recursively.

Note that headers are treated case insensitively as per RFC 2616.

<code>
$req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar'
$req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz'
$req->setHeader(array('foo' => 'Quux')); // sets the value of 'Foo' header to 'Quux'
$req->setHeader('FOO'); // removes 'Foo' header from request
</code>

@param string|array      $name    header name, header string ('Header: value')
or an array of headers
@param string|array|null $value   header value if $name is not an array,
header will be removed if value is null
@param bool              $replace whether to replace previous header with the
same name or append to its value

@return   HTTP_Request2
@throws   HTTP_Request2_LogicException