ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
54  public function __construct($a_url = '')
55  {
56  $this->url = $a_url;
57 
58  if(!self::_isCurlExtensionLoaded())
59  {
60  throw new ilCurlConnectionException('Curl extension not enabled.');
61  }
62  }
63 
71  public static final function _isCurlExtensionLoaded()
72  {
73  if(!function_exists('curl_init'))
74  {
75  return false;
76  }
77  return true;
78  }
79 
84  public function getResponseHeader()
85  {
86  return $this->header_plain;
87  }
88 
93  public function getResponseHeaderArray()
94  {
95  return (array) $this->header_arr;
96  }
97 
105  public final function init()
106  {
107  if(strlen($this->url))
108  {
109  $this->ch = curl_init($this->url);
110  #$GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->url);
111  }
112  else
113  {
114  $this->ch = curl_init();
115  }
116  if(!$this->ch)
117  {
118  throw new ilCurlConnectionException('Cannot init curl connection.');
119  }
120  if(curl_errno($this->ch))
121  {
122  throw new ilCurlConnectionException(curl_error($this->ch), curl_errno($this->ch));
123  }
124 
125  return true;
126  }
127 
137  public final function setOpt($a_option, $a_value)
138  {
139  if(!@curl_setopt($this->ch, $a_option, $a_value))
140  {
141  throw new ilCurlConnectionException('Invalid option given for: ' . $a_option, curl_errno($this->ch));
142  }
143  return true;
144  }
145 
153  public final function exec()
154  {
155  // Add header function
156  curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
157  if((@$res = curl_exec($this->ch)) === false)
158  {
159  if(strlen($err = curl_error($this->ch)))
160  {
161  throw new ilCurlConnectionException($err, curl_errno($this->ch));
162  }
163  else
164  {
165  throw new ilCurlConnectionException('Error calling curl_exec().');
166  }
167  }
168  return $res;
169  }
170 
179  public function getInfo($opt = 0)
180  {
181  if($opt)
182  {
183  $res = curl_getinfo($this->ch, $opt);
184  }
185  else
186  {
187  $res = curl_getinfo($this->ch);
188  }
189  return $res;
190  }
191 
198  private function parseHeader($handle,$header)
199  {
200  $this->header_plain = $header;
201 
202  $lines = explode('\r\n',$this->getResponseHeader());
203  foreach($lines as $line)
204  {
205  list($name,$value) = explode(':',$line,2);
206  $this->header_arr[$name] = $value;
207  }
208  return strlen($this->getResponseHeader());
209  }
210 
217  public final function close()
218  {
219  if($this->ch != null)
220  {
221  curl_close($this->ch);
222  }
223  }
224 
232  public function __destruct()
233  {
234  $this->close();
235  }
236 
237 }
238 ?>