コード例 #1
0
 /**
  * Construct the response
  *
  * @param string                              $statusCode The response status code (e.g. 200, 404, etc)
  * @param ToArrayInterface|array              $headers    The response headers
  * @param string|resource|EntityBodyInterface $body       The body of the response
  *
  * @throws BadResponseException if an invalid response code is given
  */
 public function __construct($statusCode, $headers = null, $body = null)
 {
     parent::__construct();
     $this->setStatus($statusCode);
     $this->body = EntityBody::factory($body !== null ? $body : '');
     if ($headers) {
         if (is_array($headers)) {
             $this->setHeaders($headers);
         } elseif ($headers instanceof ToArrayInterface) {
             $this->setHeaders($headers->toArray());
         } else {
             throw new BadResponseException('Invalid headers argument received');
         }
     }
 }
コード例 #2
0
 /**
  * @param string           $method  HTTP method
  * @param string|Url       $url     HTTP URL to connect to. The URI scheme, host header, and URI are parsed from the
  *                                  full URL. If query string parameters are present they will be parsed as well.
  * @param array|Collection $headers HTTP headers
  */
 public function __construct($method, $url, $headers = array())
 {
     parent::__construct();
     $this->method = strtoupper($method);
     $this->curlOptions = new Collection();
     $this->setUrl($url);
     if ($headers) {
         // Special handling for multi-value headers
         foreach ($headers as $key => $value) {
             // Deal with collisions with Host and Authorization
             if ($key == 'host' || $key == 'Host') {
                 $this->setHeader($key, $value);
             } elseif ($value instanceof HeaderInterface) {
                 $this->addHeader($key, $value);
             } else {
                 foreach ((array) $value as $v) {
                     $this->addHeader($key, $v);
                 }
             }
         }
     }
     $this->setState(self::STATE_NEW);
 }