ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCurlConnection.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
36 include_once('Services/WebServices/Curl/classes/class.ilCurlConnectionException.php');
37 
39 {
40  protected $url = '';
41  protected $ch = null;
42 
51  public function __construct($a_url = '')
52  {
53  $this->url = $a_url;
54 
55  if(!self::_isCurlExtensionLoaded())
56  {
57  throw new ilCurlConnectionException('Curl extension not enabled.');
58  }
59  }
60 
68  public static final function _isCurlExtensionLoaded()
69  {
70  if(!function_exists('curl_init'))
71  {
72  return false;
73  }
74  return true;
75  }
76 
84  public final function init()
85  {
86  if(strlen($this->url))
87  {
88  $this->ch = curl_init($this->url);
89  }
90  else
91  {
92  $this->ch = curl_init();
93  }
94  if(!$this->ch)
95  {
96  throw new ilCurlConnectionException('Cannot init curl connection.');
97  }
98  if(curl_errno($this->ch))
99  {
100  throw new ilCurlConnectionException(curl_error($this->ch),curl_errno($this->ch));
101  }
102 
103  return true;
104  }
105 
115  public final function setOpt($a_option,$a_value)
116  {
117  if(!@curl_setopt($this->ch,$a_option,$a_value))
118  {
119  throw new ilCurlConnectionException('Invalid option given for: '.$a_option,curl_errno($this->ch));
120  }
121  return true;
122  }
123 
131  public final function exec()
132  {
133  if((@$res = curl_exec($this->ch)) === false)
134  {
135  if(strlen($err = curl_error($this->ch)))
136  {
137  throw new ilCurlConnectionException($err,curl_errno($this->ch));
138  }
139  else
140  {
141  throw new ilCurlConnectionException('Error calling curl_exec().');
142  }
143  }
144  return $res;
145  }
146 
155  public function getInfo($opt = 0)
156  {
157  if($opt)
158  {
159  $res = curl_getinfo($this->ch,$opt);
160  }
161  else
162  {
163  $res = curl_getinfo($this->ch);
164  }
165  return $res;
166  }
167 
174  public final function close()
175  {
176  if($this->ch != null)
177  {
178  curl_close($this->ch);
179  }
180  }
181 
189  public function __destruct()
190  {
191  $this->close();
192  }
193 }
194 
195 ?>