EGOCMS  18.0
EGOTEC Content-Managament-System
Ego_Http.php
gehe zur Dokumentation dieser Datei
1 <?php
2 
3 class Ego_Http {
4  private $_requestHeader = array();
5  public $responseHeader = array();
6  protected $info = array();
7  public $code = 0;
8  public $responseTime = 0;
9  public $content = null;
10  public $cookie = array();
11  public $error = null;
12  public $verifySSL = true;
13  public $json = null;
14  public $use_http_build_query = true;
15  private $_proxy = array();
16 
23  public function addHeader($headers) {
24  foreach ($headers as $key => $value) {
25  $this->_requestHeader[$key] = $value;
26  }
27  }
28 
36  public function auth($username, $password = '') {
37  $this->addHeader(array(
38  'Authorization' => 'Basic '.base64_encode("$username:$password")
39  ));
40  }
41 
53  public function proxy($host, $port, $ssl = false, $username = '', $password = '', $insecure = false) {
54  $this->_proxy['host'] = $host;
55  $this->_proxy['port'] = (int) $port;
56  $this->_proxy['ssl'] = (bool) $ssl;
57  $this->_proxy['username'] = $username;
58  $this->_proxy['password'] = $password;
59  $this->_proxy['insecure'] = $insecure;
60  }
61 
69  public function get($url, $additional_header = array()) {
70  $this->request(array(
71  'url' => $url,
72  'method' => 'GET',
73  'header' => $additional_header
74  ));
75  }
76 
85  public function post($url, $param = array(), $additional_header = array()) {
86  $this->request(array(
87  'url' => $url,
88  'method' => 'POST',
89  'param' => $param,
90  'header' => $additional_header
91  ));
92  }
93 
103  public function download($url, $target, $param = array(), $additional_header = array()) {
104  $this->request(array(
105  'url' => $url,
106  'method' => 'POST',
107  'download' => $target,
108  'param' => $param,
109  'header' => $additional_header
110  ));
111  }
112 
122  public function upload($url, $file, $param = array(), $additional_header = array()) {
123  $this->request(array(
124  'url' => $url,
125  'method' => 'POST',
126  'upload' => $file,
127  'param' => $param,
128  'header' => $additional_header
129  ));
130  }
131 
138  public function request($request) {
139  $header = array();
140  foreach (array_merge($this->_requestHeader, $request['header']) as $key => $value) {
141  $header[] = $key.": ".$value;
142  }
143 
144  $ch = curl_init();
145  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
146  curl_setopt($ch, CURLOPT_URL, $request['url']);
147  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request['method']);
148 
149  // Download
150  if ($request['download']) {
151  $fp = fopen($request['download'], 'w+');
152  curl_setopt($ch, CURLOPT_FILE, $fp);
153  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
154  curl_setopt($ch, CURLOPT_TIMEOUT, 900);
155  } else {
156  curl_setopt($ch, CURLOPT_HEADER, true);
157  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
158  }
159 
160  // Don't verify SSL certificates.
161  if (!$this->verifySSL) {
162  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
163  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
164  }
165 
166  // Upload
167  if ($request['upload']) {
168  curl_setopt($ch, CURLOPT_INFILE, $request['upload']);
169  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($request['upload']));
170  }
171 
172  // URL Parameter
173  if (!empty($request['param'])) {
174  assert($request['method'] === 'POST', "Makes only sense with POST.");
175  if ($this->use_http_build_query) {
176  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request['param']));
177  } else {
178  curl_setopt($ch, CURLOPT_POSTFIELDS, $request['param']);
179  }
180  }
181 
182  // Proxy
183  if (!empty($this->_proxy)) {
184  curl_setopt($ch, CURLOPT_PROXY, $this->_proxy['host']);
185  curl_setopt($ch, CURLOPT_PROXYPORT, $this->_proxy['port']);
186  if ($this->_proxy['ssl']) {
187  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
188  }
189  if (!empty($this->_proxy['username'])) {
190  curl_setopt($ch, CURLOPT_PROXYUSERPWD, implode(':', array(
191  $this->_proxy['username'],
192  $this->_proxy['password']
193  )));
194  }
195  if ($this->_proxy['insecure']) {
196  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
197  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
198  }
199  }
200 
201  $response = curl_exec($ch);
202  if (!$response) {
203  $this->error = curl_error($ch);
204  return;
205  }
206  $this->error = null;
207 
208  if (!$request['download']) {
209  // Generate Response Headers
210  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
211  $headers = substr($response, 0, $header_size);
212  $content_orig = substr($response, $header_size);
213  $this->responseHeader = array();
214  foreach (explode("\n", $headers) as $value) {
215  $pos = strpos($value, ":");
216  $key = substr($value, 0, $pos);
217  $val = trim(substr($value, $pos + 1));
218  $this->responseHeader[$key] = $val;
219  }
220 
221  // Collect Cookies
222  $this->cookie = array();
223  if ($this->responseHeader['Set-Cookie']) {
224  $cookies = explode("\n", $this->responseHeader['Set-Cookie']);
225  foreach ($cookies as $line) {
226  $row = explode(";", $line);
227  $row0 = array_shift($row);
228  $row0_keyval = explode("=", $row0);
229  $cookie = array('value' => $row0_keyval[1]);
230  foreach ($row as $s) {
231  $keyval = explode("=", $s);
232  $cookie[trim($keyval[0])] = $keyval[1];
233  }
234  $this->cookie[$row0_keyval[0]] = $cookie;
235  }
236  }
237  }
238 
239  $this->info = curl_getinfo($ch);
240  $this->responseTime = $this->info['total_time'];
241  $this->content = $this->responseHeader['Content-Encoding']=='gzip' ? zlib_decode($content_orig) : $content_orig;
242 
243  // decode JSON
244  if (strpos($this->responseHeader['Content-Type'], "application/json") !== false) {
245  $this->json = json_decode($this->content);
246  } else {
247  $this->json = null;
248  }
249 
250  $this->code = $this->info['http_code'];
251 
252  curl_close($ch);
253  if (isset($fp)) {
254  fclose($fp);
255  }
256  }
257 }
258 ?>
addHeader($headers)
Definition: Ego_Http.php:23
post($url, $param=array(), $additional_header=array())
Definition: Ego_Http.php:85
$responseTime
Definition: Ego_Http.php:8
$responseHeader
Definition: Ego_Http.php:5
upload($url, $file, $param=array(), $additional_header=array())
Definition: Ego_Http.php:122
auth($username, $password='')
Definition: Ego_Http.php:36
$use_http_build_query
Definition: Ego_Http.php:14
download($url, $target, $param=array(), $additional_header=array())
Definition: Ego_Http.php:103
request($request)
Definition: Ego_Http.php:138
proxy($host, $port, $ssl=false, $username='', $password='', $insecure=false)
Definition: Ego_Http.php:53