Zend_Http_Client::_prepareHeaders PHP Method

_prepareHeaders() protected method

Prepare the request headers
protected _prepareHeaders ( ) : array
return array
    protected function _prepareHeaders()
    {
        $headers = array();
        // Set the host header
        if (!isset($this->headers['host'])) {
            $host = $this->uri->getHost();
            // If the port is not default, add it
            if (!($this->uri->getScheme() == 'http' && $this->uri->getPort() == 80 || $this->uri->getScheme() == 'https' && $this->uri->getPort() == 443)) {
                $host .= ':' . $this->uri->getPort();
            }
            $headers[] = "Host: {$host}";
        }
        // Set the connection header
        if (!isset($this->headers['connection'])) {
            if (!$this->config['keepalive']) {
                $headers[] = "Connection: close";
            }
        }
        // Set the Accept-encoding header if not set - depending on whether
        // zlib is available or not.
        if (!isset($this->headers['accept-encoding'])) {
            if (function_exists('gzinflate')) {
                $headers[] = 'Accept-encoding: gzip, deflate';
            } else {
                $headers[] = 'Accept-encoding: identity';
            }
        }
        // Set the Content-Type header
        if (($this->method == self::POST || $this->method == self::PUT) && (!isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) {
            $headers[] = self::CONTENT_TYPE . ': ' . $this->enctype;
        }
        // Set the user agent header
        if (!isset($this->headers['user-agent']) && isset($this->config['useragent'])) {
            $headers[] = "User-Agent: {$this->config['useragent']}";
        }
        // Set HTTP authentication if needed
        if (is_array($this->auth)) {
            $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'], $this->auth['type']);
            $headers[] = "Authorization: {$auth}";
        }
        // Load cookies from cookie jar
        if (isset($this->cookiejar)) {
            $cookstr = $this->cookiejar->getMatchingCookies($this->uri, true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
            if ($cookstr) {
                $headers[] = "Cookie: {$cookstr}";
            }
        }
        // Add all other user defined headers
        foreach ($this->headers as $header) {
            list($name, $value) = $header;
            if (is_array($value)) {
                $value = implode(', ', $value);
            }
            $headers[] = "{$name}: {$value}";
        }
        return $headers;
    }

Usage Example

Example #1
0
 /**
  * Prepare the request headers
  *
  * @return array
  */
 protected function _prepareHeaders()
 {
     $headers = parent::_prepareHeaders();
     // Set the Content-Type header
     if (($this->method == self::POST || $this->method == self::PUT) && (!isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype) || isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype) && ($this->enctype !== self::ENC_URLENCODED || $this->enctype !== self::ENC_FORMDATA))) {
         if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) {
             $ContentTypeHeader = $this->headers[strtolower(self::CONTENT_TYPE)];
             switch ($ContentTypeHeader[1]) {
                 case parent::ENC_FORMDATA:
                     $enctype = self::ENC_FORMDATA;
                     break;
                 case parent::ENC_URLENCODED:
                     $enctype = self::ENC_URLENCODED;
                     break;
                 default:
                     $enctype = self::CONTENT_TYPE;
                     break;
             }
             unset($this->headers[strtolower(self::CONTENT_TYPE)]);
         } else {
             $enctype = $this->enctype;
         }
         foreach ($headers as $key => $val) {
             if (0 === strpos($val, self::CONTENT_TYPE)) {
                 unset($headers[$key]);
                 break;
             }
         }
         $headers[] = self::CONTENT_TYPE . ': ' . $enctype;
     }
     return $headers;
 }