• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/WebServices/Curl/classes/class.ilCurlConnection.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00036 include_once('Services/WebServices/Curl/classes/class.ilCurlConnectionException.php');
00037 
00038 class ilCurlConnection
00039 {
00040         protected $url = '';
00041         protected $ch = null;
00042 
00051         public function __construct($a_url = '')
00052         {
00053                 $this->url = $a_url;
00054                 
00055                 if(!self::_isCurlExtensionLoaded())
00056                 {
00057                         throw new ilCurlConnectionException('Curl extension not enabled.');
00058                 }
00059         }
00060         
00068         public static final function _isCurlExtensionLoaded()
00069         {
00070                 if(!function_exists('curl_init'))
00071                 {
00072                         return false;
00073                 }
00074                 return true;
00075         }
00076         
00084         public final function init()
00085         {
00086                 if(strlen($this->url))
00087                 {
00088                         $this->ch = curl_init($this->url);
00089                 }
00090                 else
00091                 {
00092                         $this->ch = curl_init();
00093                 }
00094                 if(!$this->ch)
00095                 {
00096                         throw new ilCurlConnectionException('Cannot init curl connection.');
00097                 }
00098                 if(curl_errno($this->ch))
00099                 {
00100                         throw new ilCurlConnectionException(curl_error($this->ch),curl_errno($this->ch));
00101                 }
00102                 
00103                 return true;
00104         }
00105         
00115         public final function setOpt($a_option,$a_value)
00116         {
00117                 if(!@curl_setopt($this->ch,$a_option,$a_value))
00118                 {
00119                         throw new ilCurlConnectionException('Invalid option given for: '.$a_option,curl_errno($this->ch));
00120                 }
00121                 return true;
00122         }
00123         
00131         public final function exec()
00132         {
00133                 if((@$res = curl_exec($this->ch)) === false)
00134                 {
00135                         if(strlen($err = curl_error($this->ch)))
00136                         {
00137                                 throw new ilCurlConnectionException($err,curl_errno($this->ch));
00138                         }
00139                         else
00140                         {
00141                                 throw new ilCurlConnectionException('Error calling curl_exec().');
00142                         }
00143                 }
00144                 return $res;
00145         }
00146         
00153         public final function close()
00154         {
00155                 if($this->ch != null)
00156                 {
00157                         curl_close($this->ch);
00158                 }
00159         }
00160         
00168         public function __destruct()
00169         {
00170                 $this->close();
00171         }
00172 }
00173 
00174 ?>

Generated on Fri Dec 13 2013 17:57:02 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1