Zend_Http_Client::setCookie PHP Method

setCookie() public method

Add a cookie to the request. If the client has no Cookie Jar, the cookies will be added directly to the headers array as "Cookie" headers.
public setCookie ( Zend_Http_Cookie | string $cookie, string | null $value = null ) : Zend_Http_Client
$cookie Zend_Http_Cookie | string
$value string | null If "cookie" is a string, this is the cookie value.
return Zend_Http_Client
    public function setCookie($cookie, $value = null)
    {
        Zend_Loader::loadClass('Zend_Http_Cookie');
        if (is_array($cookie)) {
            foreach ($cookie as $c => $v) {
                if (is_string($c)) {
                    $this->setCookie($c, $v);
                } else {
                    $this->setCookie($v);
                }
            }
            return $this;
        }
        if ($value !== null && $this->config['encodecookies']) {
            $value = urlencode($value);
        }
        if (isset($this->cookiejar)) {
            if ($cookie instanceof Zend_Http_Cookie) {
                $this->cookiejar->addCookie($cookie);
            } elseif (is_string($cookie) && $value !== null) {
                $cookie = Zend_Http_Cookie::fromString("{$cookie}={$value}", $this->uri, $this->config['encodecookies']);
                $this->cookiejar->addCookie($cookie);
            }
        } else {
            if ($cookie instanceof Zend_Http_Cookie) {
                $name = $cookie->getName();
                $value = $cookie->getValue();
                $cookie = $name;
            }
            if (preg_match("/[=,; \t\r\n\v\f]/", $cookie)) {
                /** @see Zend_Http_Client_Exception */
                require_once 'Zend/Http/Client/Exception.php';
                throw new Zend_Http_Client_Exception("Cookie name cannot contain these characters: =,; \t\r\n\v\f ({$cookie})");
            }
            $value = addslashes($value);
            if (!isset($this->headers['cookie'])) {
                $this->headers['cookie'] = array('Cookie', '');
            }
            $this->headers['cookie'][1] .= $cookie . '=' . $value . '; ';
        }
        return $this;
    }

Usage Example

 public function logoutAction()
 {
     $serverUrl = 'http://' . AUTH_SERVER . self::AUTH_PATH . '/logout';
     $client = new Zend_Http_Client($serverUrl, array('timeout' => 30));
     try {
         if (isset($_COOKIE[self::AUTH_SID])) {
             $moduleName = $this->getRequest()->getModuleName();
             $client->setCookie('PHPSESSID', $_COOKIE[self::AUTH_SID]);
             $client->setCookie('moduleName', $moduleName);
             $response = $client->request();
             if ($response->isError()) {
                 $remoteErr = $response->getStatus() . ' : ' . $response->getMessage() . '<br/>' . $response->getBody();
                 throw new Zend_Exception($remoteErr, Zend_Log::ERR);
             }
         } else {
             $this->_helper->logger('No remote cookie found');
         }
     } catch (Zend_Exception $e) {
         echo $e->getMessage();
     }
     if (isset($_COOKIE[self::AUTH_SID])) {
         preg_match('/[^.]+\\.[^.]+$/', $_SERVER['SERVER_NAME'], $domain);
         setcookie(self::AUTH_SID, '', time() - 360000, self::AUTH_PATH, ".{$domain['0']}");
     }
     Zend_Auth::getInstance()->clearIdentity();
     Zend_Session::destroy();
 }
All Usage Examples Of Zend_Http_Client::setCookie