ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilHttpRequest.php
Go to the documentation of this file.
1 <?php
2 /*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22 */
23 
34 {
35  public $_fp; // HTTP socket
36  public $_url; // full URL
37  public $_host; // HTTP host
38  public $_protocol; // protocol (HTTP/HTTPS)
39  public $_uri; // request URI
40  public $_port; // port
41 
42  // scan url
43  public function _scan_url()
44  {
45  $req = $this->_url;
46 
47  $pos = strpos($req, '://');
48  $this->_protocol = strtolower(substr($req, 0, $pos));
49 
50  $req = substr($req, $pos+3);
51  $pos = strpos($req, '/');
52  if ($pos === false) {
53  $pos = strlen($req);
54  }
55  $host = substr($req, 0, $pos);
56 
57  if (strpos($host, ':') !== false) {
58  list($this->_host, $this->_port) = explode(':', $host);
59  } else {
60  $this->_host = $host;
61  $this->_port = ($this->_protocol == 'https') ? 443 : 80;
62  }
63 
64  $this->_uri = substr($req, $pos);
65  if ($this->_uri == '') {
66  $this->_uri = '/';
67  }
68  }
69 
70  // constructor
71  public function __construct($url)
72  {
73  $this->_url = $url;
74  $this->_scan_url();
75  }
76 
77  // download URL to string
78  public function downloadToString()
79  {
80  $crlf = "\r\n";
81 
82  // generate request
83  $req = 'GET ' . $this->_uri . ' HTTP/1.0' . $crlf
84  . 'Host: ' . $this->_host . $crlf
85  . $crlf;
86 
87  // fetch
88  $this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port);
89  fwrite($this->_fp, $req);
90  while (is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) {
91  $response .= fread($this->_fp, 1024);
92  }
93  fclose($this->_fp);
94 
95  // split header and body
96  $pos = strpos($response, $crlf . $crlf);
97  if ($pos === false) {
98  return($response);
99  }
100  $header = substr($response, 0, $pos);
101  $body = substr($response, $pos + 2 * strlen($crlf));
102 
103  // parse headers
104  $headers = array();
105  $lines = explode($crlf, $header);
106  foreach ($lines as $line) {
107  if (($pos = strpos($line, ':')) !== false) {
108  $headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));
109  }
110  }
111 
112  // redirection?
113  if (isset($headers['location'])) {
114  $http = new ilHttpRequest($headers['location']);
115  return($http->DownloadToString($http));
116  } else {
117  return($body);
118  }
119  }
120 }
$req
Definition: getUserInfo.php:20
ilHttpRequest class
$http
Definition: raiseError.php:7
Create styles array
The data for the language used.
$url
$response