Zend_Http_Client::setHeaders PHP Method

setHeaders() public method

This function can be used in several ways to set the client's request headers: 1. By providing two parameters: $name as the header to set (e.g. 'Host') and $value as it's value (e.g. 'www.example.com'). 2. By providing a single header string as the only parameter e.g. 'Host: www.example.com' 3. By providing an array of headers as the first parameter e.g. array('host' => 'www.example.com', 'x-foo: bar'). In This case the function will call itself recursively for each array item.
public setHeaders ( string | array $name, mixed $value = null ) : Zend_Http_Client
$name string | array Header name, full header string ('Header: value') or an array of headers
$value mixed Header value or null
return Zend_Http_Client
    public function setHeaders($name, $value = null)
    {
        // If we got an array, go recursive!
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                if (is_string($k)) {
                    $this->setHeaders($k, $v);
                } else {
                    $this->setHeaders($v, null);
                }
            }
        } else {
            // Check if $name needs to be split
            if ($value === null && strpos($name, ':') > 0) {
                list($name, $value) = explode(':', $name, 2);
            }
            // Make sure the name is valid if we are in strict mode
            if ($this->config['strict'] && !preg_match('/^[a-zA-Z0-9-]+$/', $name)) {
                /** @see Zend_Http_Client_Exception */
                require_once 'Zend/Http/Client/Exception.php';
                throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name");
            }
            $normalized_name = strtolower($name);
            // If $value is null or false, unset the header
            if ($value === null || $value === false) {
                unset($this->headers[$normalized_name]);
                // Else, set the header
            } else {
                // Header names are stored lowercase internally.
                if (is_string($value)) {
                    $value = trim($value);
                }
                $this->headers[$normalized_name] = array($name, $value);
            }
        }
        return $this;
    }

Usage Example

Beispiel #1
0
 protected function _api_request($method, $path, $data = null)
 {
     $url = trim($this->getBaseApiUrl(), "/") . self::API_CHECKOUT_PATH . $path;
     $client = new Zend_Http_Client($url);
     if (in_array($method, array(Zend_Http_Client::POST, Zend_Http_Client::PUT, 'PATCH')) && $data) {
         $client->setHeaders('Content-type: application/json');
         $client->setRawData(json_encode($data), 'application/json');
     }
     $client->setHeaders('Authorization: Bearer ' . Mage::getStoreConfig('payment/aplazame/secret_api_key'));
     $client->setHeaders('User-Agent: ' . self::USER_AGENT);
     $client->setHeaders('Accept: ' . 'application/vnd.aplazame' . (Mage::getStoreConfig('payment/aplazame/sandbox') ? '.sandbox-' : '-') . Mage::getStoreConfig('payment/aplazame/version') . '+json');
     $response = $client->request($method);
     $raw_result = $response->getBody();
     $status_code = $response->getStatus();
     if ($status_code >= 500) {
         Mage::throwException(Mage::helper('aplazame')->__('Aplazame error code: ' . $status_code));
     }
     try {
         $ret_json = Zend_Json::decode($raw_result, Zend_Json::TYPE_ARRAY);
     } catch (Zend_Json_Exception $e) {
         Mage::throwException(Mage::helper('aplazame')->__('Invalid api response: ' . $raw_result));
     }
     if ($status_code >= 400) {
         Mage::throwException(Mage::helper('aplazame')->__('Aplazame error code ' . $status_code . ': ' . $ret_json['error']['message']));
     }
     return $ret_json;
 }
All Usage Examples Of Zend_Http_Client::setHeaders