Phergie_Plugin_Http_Response::setContent PHP Method

setContent() public method

Sets the content of the response body.
public setContent ( mixed $content ) : Phergie_Plugin_Http_Response
$content mixed Response body content
return Phergie_Plugin_Http_Response Provides a fluent interface
    public function setContent($content)
    {
        $this->content = $content;
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: Http.php プロジェクト: Bittarman/phergie
 /**
  * Supporting method that executes a request and handles the response.
  *
  * @param string $url URL to request
  * @param array $context Associative array of stream context parameters 
  *
  * @return Phergie_Plugin_Http_Response Object representing the response 
  *         resulting from the request
  */
 public function request($url, array $context)
 {
     $this->response = new Phergie_Plugin_Http_Response();
     $url = (string) $url;
     $context = stream_context_create(array('http' => $context));
     set_error_handler(array($this, 'handleError'), E_WARNING);
     $stream = fopen($url, 'r', false, $context);
     if ($stream) {
         $meta = stream_get_meta_data($stream);
         $status = $this->parseStatusLine($meta['wrapper_data'][0]);
         $code = $status['code'];
         $message = $status['message'];
         $headers = array();
         foreach (array_slice($meta['wrapper_data'], 1) as $header) {
             list($name, $value) = explode(': ', $header, 2);
             $headers[$name] = $value;
         }
         unset($meta['wrapper_data']);
         $this->response->setCode($code)->setMessage($message)->setHeaders($headers)->setMeta($meta);
         $body = stream_get_contents($stream);
         $type = $this->response->getHeaders('content-type');
         foreach ($this->handlers as $expr => $handler) {
             if (preg_match('#^' . $expr . '$#i', $type)) {
                 $body = call_user_func($handler, $body);
             }
         }
         $this->response->setContent($body);
     }
     restore_error_handler();
     return $this->response;
 }