Overview

Packages

  • Http

Classes

  • Horde_Http
  • Horde_Http_Client
  • Horde_Http_Exception
  • Horde_Http_Request_Base
  • Horde_Http_Request_Curl
  • Horde_Http_Request_Factory
  • Horde_Http_Request_Fopen
  • Horde_Http_Request_Mock
  • Horde_Http_Request_Peclhttp
  • Horde_Http_Response_Base
  • Horde_Http_Response_Curl
  • Horde_Http_Response_Fopen
  • Horde_Http_Response_Mock
  • Horde_Http_Response_Peclhttp
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * Resources:
  6:  * http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
  7:  *
  8:  * @author   Chuck Hagenbuch <chuck@horde.org>
  9:  * @license  http://www.horde.org/licenses/bsd BSD
 10:  * @category Horde
 11:  * @package  Http
 12:  */
 13: 
 14: /**
 15:  * @author   Chuck Hagenbuch <chuck@horde.org>
 16:  * @license  http://www.horde.org/licenses/bsd BSD
 17:  * @category Horde
 18:  * @package  Http
 19:  */
 20: class Horde_Http_Request_Fopen extends Horde_Http_Request_Base
 21: {
 22:     /**
 23:      * Constructor
 24:      *
 25:      * @throws Horde_Http_Exception
 26:      */
 27:     public function __construct($args = array())
 28:     {
 29:         if (!ini_get('allow_url_fopen')) {
 30:             throw new Horde_Http_Exception('allow_url_fopen must be enabled');
 31:         }
 32: 
 33:         parent::__construct($args);
 34:     }
 35: 
 36:     /**
 37:      * Send this HTTP request
 38:      *
 39:      * @throws Horde_Http_Exception
 40:      * @return Horde_Http_Response_Base
 41:      */
 42:     public function send()
 43:     {
 44:         $method = $this->method;
 45:         $uri = $this->uri;
 46:         $headers = $this->headers;
 47:         $data = $this->data;
 48:         if (is_array($data)) {
 49:             $data = http_build_query($data, '', '&');
 50:         }
 51: 
 52:         $opts = array('http' => array());
 53: 
 54:         // Proxy settings
 55:         if ($this->proxyServer) {
 56:             $opts['http']['proxy'] = 'tcp://' . $this->proxyServer;
 57:             if ($this->proxyPort) {
 58:                 $opts['http']['proxy'] .= ':' . $this->proxyPort;
 59:             }
 60:             $opts['http']['request_fulluri'] = true;
 61:             if ($this->proxyUsername && $this->proxyPassword) {
 62:                 // @TODO check $this->proxyAuthenticationScheme
 63:                 $headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->proxyUsername . ':' . $this->proxyPassword);
 64:             }
 65:             if ($this->proxyType != Horde_Http::PROXY_HTTP) {
 66:                 throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
 67:             }
 68:         }
 69: 
 70:         // Authentication settings
 71:         if ($this->username) {
 72:             switch ($this->authenticationScheme) {
 73:             case Horde_Http::AUTH_BASIC:
 74:             case Horde_Http::AUTH_ANY:
 75:                 $headers['Authorization'] = 'Basic ' . base64_encode($this->username . ':' . $this->password);
 76:                 break;
 77: 
 78:             default:
 79:                 throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')');
 80:             }
 81:         }
 82: 
 83:         // Concatenate the headers
 84:         $hdr = array();
 85:         foreach ($headers as $header => $value) {
 86:             $hdr[] = $header . ': ' . $value;
 87:         }
 88: 
 89:         // Stream context config.
 90:         $opts['http']['method'] = $method;
 91:         $opts['http']['header'] = implode("\n", $hdr);
 92:         $opts['http']['content'] = $data;
 93:         $opts['http']['timeout'] = $this->timeout;
 94:         $opts['http']['max_redirects'] = $this->redirects;
 95:         $opts['http']['ignore_errors'] = true;
 96: 
 97:         $context = stream_context_create($opts);
 98:         $stream = @fopen($uri, 'rb', false, $context);
 99:         if (!$stream) {
100:             $error = error_get_last();
101:             if (preg_match('/HTTP\/(\d+\.\d+) (\d{3}) (.*)$/', $error['message'], $matches)) {
102:                 // Create a Response for the HTTP error code
103:                 return new Horde_Http_Response_Fopen($uri, null, $matches[0]);
104:             } else {
105:                 throw new Horde_Http_Exception('Problem with ' . $uri . ': ', $error);
106:             }
107:         }
108: 
109:         $meta = stream_get_meta_data($stream);
110:         $headers = isset($meta['wrapper_data']) ? $meta['wrapper_data'] : array();
111: 
112:         return new Horde_Http_Response_Fopen($uri, $stream, $headers);
113:     }
114: }
115: 
API documentation generated by ApiGen