Overview

Namespaces

  • Mapbender
    • Component
      • HTTP
    • CoreBundle
      • Command
      • Component
        • Exception
      • Controller
      • DataFixtures
        • ORM
      • DependencyInjection
      • Element
        • Type
      • Entity
      • EventListener
      • Extension
      • Form
        • DataTransformer
        • EventListener
        • Type
      • Security
      • Template
    • KmlBundle
      • Element
    • ManagerBundle
      • Controller
      • Form
        • DataTransformer
        • Type
    • MonitoringBundle
      • Command
      • Component
      • Controller
      • DependencyInjection
      • Entity
      • EventListener
      • Form
    • PrintBundle
      • Component
      • Controller
    • WmcBundle
      • Component
        • Exception
      • Element
        • Type
      • Entity
      • Form
        • Type
    • WmsBundle
      • Component
        • Exception
      • Controller
      • DependencyInjection
      • Element
        • Type
      • Entity
      • Event
      • Form
        • EventListener
        • Type
    • WmtsBundle
      • Component
        • Exception
      • Controller
      • Entity
      • Form
        • Type
  • None
  • PHP

Classes

  • HTTPClient
  • HTTPResult
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: namespace Mapbender\Component\HTTP;
  4: 
  5: class HTTPClient {
  6: 
  7:     protected $container = "";
  8:     protected $ch = null;
  9:     protected $method = "GET";
 10:     protected $headers = array();
 11:     protected $host = "";
 12:     protected $port = "";
 13:     protected $path = "";
 14:     protected $username = null;
 15:     protected $password = null;
 16:     protected $proxyHost = null;
 17:     protected $proxyPort = null;
 18:     protected $noProxyHosts = array();
 19:     protected $usrpwd = null;
 20: 
 21:     public function __construct($container = null) {
 22:         $this->ch = curl_init();
 23:         $this->container = $container;
 24:         if ($this->container) {
 25:             try {
 26:                 $proxyConf = $this->container->getParameter('mapbender.proxy');
 27:             } catch (\InvalidArgumentException $E) {
 28: // thrown when the parameter is not set
 29: // maybe some logging ?
 30:                 $proxyConf = array();
 31:                 $this->container->get('logger')->debug('Not using Proxy Configuuration');
 32:             }
 33:             if (isset($proxyConf['host']) && $proxyConf['host'] != "") {
 34:                 $this->setProxyHost($proxyConf['host']);
 35:                 if (isset($proxyConf['port']) && $proxyConf['port'] != "") {
 36:                     $this->setProxyPort($proxyConf['port']);
 37:                 }
 38:                 if (isset($proxyConf['user']) && $proxyConf['user'] != "") {
 39:                     $this->setUsrPwd($proxyConf['user'] . ":" . (isset($proxyConf['password']) ? $proxyConf['password'] : null));
 40:                 }
 41:                 if (isset($proxyConf['noproxy']) && is_array($proxyConf['noproxy'])) {
 42:                     $this->setNoProxyHosts($proxyConf['noproxy']);
 43:                 } else {
 44:                     $this->setNoProxyHosts(array());
 45:                 }
 46:                 $this->container->get('logger')
 47:                         ->debug(sprintf('Making Request via Proxy: %s:%s', $this->getProxyHost(), $this->getProxyPort(), implode(",", $this->getNoProxyHosts())));
 48:             }
 49:         }
 50:     }
 51: 
 52:     public function __destruct() {
 53:         $this->ch = curl_close($this->ch);
 54:     }
 55: 
 56:     public function setProxyHost($host) {
 57:         $this->proxyHost = $host;
 58:     }
 59: 
 60:     public function getProxyHost() {
 61:         return $this->proxyHost;
 62:     }
 63: 
 64:     public function setProxyPort($port) {
 65:         $this->proxyPort = $port;
 66:     }
 67: 
 68:     public function getProxyPort() {
 69:         return $this->proxyPort;
 70:     }
 71: 
 72:     public function setNoProxyHosts($noProxyHosts) {
 73:         $this->noProxyHosts = $noProxyHosts;
 74:     }
 75: 
 76:     public function getNoProxyHosts() {
 77:         return $this->noProxyHosts;
 78:     }
 79: 
 80:     public function getUsername() {
 81:         return $this->username;
 82:     }
 83: 
 84:     public function setUsername($username) {
 85:         $this->username = $username;
 86:     }
 87: 
 88:     public function getPassword() {
 89:         return $this->password;
 90:     }
 91: 
 92:     public function setPassword($password) {
 93:         $this->password = $password;
 94:     }
 95: 
 96:     public function setUsrPwd($usrpwd) {
 97:         $this->usrpwd = $usrpwd;
 98:     }
 99: 
100:     public function getUsrPwd() {
101:         return $this->usrpwd;
102:     }
103: 
104:     /**
105:      * Shortcut Method 
106:      */
107:     public function open($url, $query = array(), $method='GET', $data='') {
108:         curl_setopt($this->ch, CURLOPT_URL, $url);
109:         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
110:         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
111:         curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
112: 
113:         $url_ = parse_url($url);
114: 
115:         if ($this->getUsrPwd() !== null && !in_array($url_['host'], $this->getNoProxyHosts())) {
116:             curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
117:             curl_setopt($this->ch, CURLOPT_USERPWD, $this->getUsrPwd());
118:         }
119:         if ($this->getProxyHost() !== null && !in_array($url_['host'], $this->getNoProxyHosts())) {
120:             curl_setopt($this->ch, CURLOPT_PROXY, $this->getProxyHost());
121:         }
122:         if ($this->getProxyPort() !== null && !in_array($url_['host'], $this->getNoProxyHosts())) {
123:             curl_setopt($this->ch, CURLOPT_PROXYPORT, $this->getProxyport());
124:         }
125: 
126:         $data = curl_exec($this->ch);
127: 
128:         if (($error = curl_error($this->ch)) != "") {
129:             throw new \Exception("Curl says: '$error'");
130:         }
131:         $statusCode = curl_getInfo($this->ch, CURLINFO_HTTP_CODE);
132: 
133:         $result = new HTTPResult();
134:         $result->setData($data);
135:         $result->setStatusCode($statusCode);
136:         return $result;
137:     }
138: 
139:     public static function parseQueryString($str) {
140:         $op = array();
141:         $pairs = explode("&", $str);
142:         foreach ($pairs as $pair) {
143:             $arr = explode("=", $pair);
144:             $k = isset($arr[0]) ? $arr[0] : null;
145:             $v = isset($arr[1]) ? $arr[1] : null;
146:             if ($k !== null) {
147:                 $op[$k] = $v;
148:             }
149:         }
150:         return $op;
151:     }
152: 
153:     public static function buildQueryString($parsedQuery) {
154:         $result = array();
155:         foreach ($parsedQuery as $key => $value) {
156:             if ($key || $value) {
157:                 $result[] = "$key=$value";
158:             }
159:         }
160:         return implode("&", $result);
161:     }
162: 
163:     public static function parseUrl($url) {
164:         $defaults = array(
165:             "scheme" => "http",
166:             "host" => null,
167:             "port" => null,
168:             "user" => null,
169:             "pass" => null,
170:             "path" => "/",
171:             "query" => null,
172:             "fragment" => null
173:         );
174: 
175:         $parsedUrl = parse_url($url);
176: 
177:         $mergedUrl = array_merge($defaults, $parsedUrl);
178:         return $mergedUrl;
179:     }
180: 
181:     public static function buildUrl(array $parsedUrl) {
182:         $defaults = array(
183:             "scheme" => "http",
184:             "host" => null,
185:             "port" => null,
186:             "user" => null,
187:             "pass" => null,
188:             "path" => "/",
189:             "query" => null,
190:             "fragment" => null
191:         );
192: 
193:         $mergedUrl = array_merge($defaults, $parsedUrl);
194: 
195:         $result = $mergedUrl['scheme'] . "://";
196: 
197:         $authString = $mergedUrl['user'];
198:         $authString .= $mergedUrl['pass'] ? ":" . $mergedUrl['pass'] : "";
199:         $authString .= $authString ? "@" : "";
200:         $result .= $authString;
201: 
202:         $result .= $mergedUrl['host'];
203:         $result .= $mergedUrl['port'] ? ':' . $mergedUrl['port'] : "";
204:         $result .= $mergedUrl['path'];
205:         $result .= $mergedUrl['query'] ? '?' . $mergedUrl['query'] : "";
206:         return $result;
207:     }
208: }
209: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0