ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilCurlConnection.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23  */
24 
36 include_once('Services/WebServices/Curl/classes/class.ilCurlConnectionException.php');
37 
39 {
40  protected $url = '';
41  protected $ch = null;
42 
43  private $header_plain = '';
44  private $header_arr = array();
45 
46  private $response_body = '';
47 
56  public function __construct($a_url = '')
57  {
58  $this->url = $a_url;
59 
60  if(!self::_isCurlExtensionLoaded())
61  {
62  throw new ilCurlConnectionException('Curl extension not enabled.');
63  }
64  }
65 
73  public static final function _isCurlExtensionLoaded()
74  {
75  if(!function_exists('curl_init'))
76  {
77  return false;
78  }
79  return true;
80  }
81 
86  public function getResponseHeader()
87  {
88  return $this->header_plain;
89  }
90 
95  public function getResponseHeaderArray()
96  {
97  return (array) $this->header_arr;
98  }
99 
107  public final function init()
108  {
109  if(strlen($this->url))
110  {
111  $this->ch = curl_init($this->url);
112  #$GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->url);
113  }
114  else
115  {
116  $this->ch = curl_init();
117  }
118  if(!$this->ch)
119  {
120  throw new ilCurlConnectionException('Cannot init curl connection.');
121  }
122  if(curl_errno($this->ch))
123  {
124  throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
125  }
126 
127  return true;
128  }
129 
139  public final function setOpt($a_option, $a_value)
140  {
141  if(!@curl_setopt($this->ch, $a_option, $a_value))
142  {
143  throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
144  }
145  return true;
146  }
147 
155  public final function exec()
156  {
157  // Add header function
158  curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
159  if((@$res = curl_exec($this->ch)) === false)
160  {
161  if(strlen($err = curl_error($this->ch)))
162  {
163  throw new ilCurlConnectionException($err, curl_errno($this->ch));
164  }
165  else
166  {
167  throw new ilCurlConnectionException('Error calling curl_exec().');
168  }
169  }
170  return $res;
171  }
172 
177  public function parseResponse($a_response)
178  {
179  $header_size = $this->getInfo(CURLINFO_HEADER_SIZE);
180 
181  $this->header_plain = substr($a_response, 0, $header_size);
182  $this->response_body = substr($a_response, $header_size);
183  }
184 
189  public function getResponseBody()
190  {
191  return $this->response_body;
192  }
193 
202  public function getInfo($opt = 0)
203  {
204  if($opt)
205  {
206  $res = curl_getinfo($this->ch, $opt);
207  }
208  else
209  {
210  $res = curl_getinfo($this->ch);
211  }
212  return $res;
213  }
214 
221  private function parseHeader($handle,$header)
222  {
223  $this->header_plain = $header;
224 
225  $lines = explode('\r\n',$this->getResponseHeader());
226  foreach($lines as $line)
227  {
228  list($name,$value) = explode(':',$line,2);
229  $this->header_arr[$name] = $value;
230  }
231  return strlen($this->getResponseHeader());
232  }
233 
240  public final function close()
241  {
242  if($this->ch != null)
243  {
244  curl_close($this->ch);
245  $this->ch = null;
246  }
247  }
248 
254  public function __destruct()
255  {
256  $this->close();
257  }
258 }
259 ?>
close()
Close connection.
parseResponse($a_response)
parse response body
exec()
Wrapper for curl_exec.
init()
Init curl connection.
getResponseBody()
Get responce body.
parseHeader($handle, $header)
Parse respone header.
__construct($a_url='')
Constructor.
static _isCurlExtensionLoaded()
Check if curl extension is loaded.
$header
getResponseHeader()
Get response header as string.
getResponseHeaderArray()
Get response header as array.
getInfo($opt=0)
Get informations about a specific transfer.
Create styles array
The data for the language used.
setOpt($a_option, $a_value)
Wrapper for curl_setopt.
__destruct()
Destructor public.