/**
  * 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.
  *
  * @param EHttpCookie|string $cookie
  * @param string|null $value If "cookie" is a string, this is the cookie value.
  * @return EHttpClient
  * @throws EHttpClientException
  */
 public function setCookie($cookie, $value = null)
 {
     if (!class_exists('EHttpCookie')) {
         require_once 'EHttpCookie.php';
     }
     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 EHttpCookie) {
             $this->cookiejar->addCookie($cookie);
         } elseif (is_string($cookie) && $value !== null) {
             $cookie = EHttpCookie::fromString("{$cookie}={$value}", $this->uri, $this->config['encodecookies']);
             $this->cookiejar->addCookie($cookie);
         }
     } else {
         if ($cookie instanceof EHttpCookie) {
             $name = $cookie->getName();
             $value = $cookie->getValue();
             $cookie = $name;
         }
         if (preg_match("/[=,; \t\r\n\v\f]/", $cookie)) {
             throw new EHttpClientException(Yii::t('EHttpClient', "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;
 }
Exemple #2
0
 /**
  * Add a cookie to the jar. Cookie should be passed either as a EHttpCookieJar object
  * or as a string - in which case an object is created from the string.
  *
  * @param EHttpCookieJar|string $cookie
  * @param EUriHttp|string    $ref_uri Optional reference URI (for domain, path, secure)
  */
 public function addCookie($cookie, $ref_uri = null, $encodeValue = true)
 {
     if (is_string($cookie)) {
         $cookie = EHttpCookie::fromString($cookie, $ref_uri, $encodeValue);
     }
     if ($cookie instanceof EHttpCookie) {
         $domain = $cookie->getDomain();
         $path = $cookie->getPath();
         if (!isset($this->cookies[$domain])) {
             $this->cookies[$domain] = array();
         }
         if (!isset($this->cookies[$domain][$path])) {
             $this->cookies[$domain][$path] = array();
         }
         $this->cookies[$domain][$path][$cookie->getName()] = $cookie;
         $this->_rawCookies[] = $cookie;
     } else {
         throw new EHttpClientException('Supplient argument is not a valid cookie string or object');
     }
 }