Go to the documentation of this file.00001 <?php
00002
00009 class oxCurl
00010 {
00016 protected $_rCurl = null;
00017
00023 protected $_sUrl = null;
00024
00030 protected $_sQuery = null;
00031
00037 protected $_sMethod = 'POST';
00038
00044 protected $_aParameters = null;
00045
00050 protected $_sConnectionCharset = "UTF-8";
00051
00056 protected $_aHeader = null;
00057
00062 protected $_sHost = null;
00063
00069 protected $_aOptions = array('CURLOPT_RETURNTRANSFER' => 1);
00070
00076 protected $_sStatusCode = null;
00077
00087 public function setUrl( $sUrl )
00088 {
00089 $this->_sUrl = $sUrl;
00090 }
00091
00097 public function getUrl()
00098 {
00099 if ($this->getMethod() == "GET" && $this->getQuery()) {
00100 $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
00101 }
00102 return $this->_sUrl;
00103 }
00104
00108 public function setQuery( $sQuery = null )
00109 {
00110 if ( is_null($sQuery) ) {
00111 $sQuery = "";
00112 if ( $aParams = $this->getParameters() ) {
00113 $aParams = $this->_prepareQueryParameters( $aParams );
00114 $sQuery = http_build_query( $aParams, "", "&" );
00115 }
00116 }
00117
00118 $this->_sQuery = $sQuery;
00119 }
00120
00126 public function getQuery()
00127 {
00128 if ( is_null( $this->_sQuery ) ) {
00129 $this->setQuery();
00130 }
00131
00132 return $this->_sQuery;
00133 }
00134
00140 public function setParameters( $aParameters )
00141 {
00142 $this->_aParameters = $aParameters;
00143 }
00144
00150 public function getParameters()
00151 {
00152 return $this->_aParameters;
00153 }
00154
00162 public function setHost( $sHost )
00163 {
00164 $this->_sHost = $sHost;
00165 }
00166
00172 public function getHost()
00173 {
00174 return $this->_sHost;
00175 }
00176
00184 public function setHeader( $aHeader = null )
00185 {
00186 if ( is_null( $aHeader ) && $this->getMethod() == "POST") {
00187 $sHost = $this->getHost();
00188
00189 $aHeader = array();
00190 $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00191 $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00192 if ( isset( $sHost ) ) {
00193 $aHeader[] = 'Host: '. $sHost;
00194 }
00195 $aHeader[] = 'Connection: close';
00196 }
00197 $this->_aHeader = $aHeader;
00198 }
00199
00205 public function getHeader()
00206 {
00207 if ( is_null( $this->_aHeader ) ) {
00208 $this->setHeader();
00209 }
00210 return $this->_aHeader;
00211 }
00212
00220 public function setMethod($sMethod)
00221 {
00222 $this->_sMethod = strtoupper($sMethod);
00223 }
00224
00230 public function getMethod()
00231 {
00232 return $this->_sMethod;
00233 }
00234
00245 public function setOption( $sName, $sValue )
00246 {
00247 if (strpos( $sName, 'CURLOPT_' ) !== 0 || !defined($sConstant = strtoupper($sName))) {
00251 $oException = oxNew( 'oxException' );
00252 $oLang = oxRegistry::getLang();
00253 $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage() ), $sName ) );
00254 throw $oException;
00255 }
00256
00257 $this->_aOptions[$sName] = $sValue;
00258 }
00259
00265 public function getOptions()
00266 {
00267 return $this->_aOptions;
00268 }
00269
00277 public function execute()
00278 {
00279 $this->_setOptions();
00280
00281 $sResponse = $this->_execute();
00282 $this->_saveStatusCode();
00283
00284 $iCurlErrorNumber = $this->_getErrorNumber();
00285
00286 $this->_close();
00287
00288 if ( $iCurlErrorNumber ) {
00292 $oException = oxNew( 'oxException' );
00293 $oLang = oxRegistry::getLang();
00294 $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_CURL_ERROR', $oLang->getTplLanguage() ), $iCurlErrorNumber ) );
00295 throw $oException;
00296 }
00297
00298 return $sResponse;
00299 }
00300
00306 public function setConnectionCharset( $sCharset )
00307 {
00308 $this->_sConnectionCharset = $sCharset;
00309 }
00310
00316 public function getConnectionCharset()
00317 {
00318 return $this->_sConnectionCharset;
00319 }
00320
00326 public function getStatusCode()
00327 {
00328 return $this->_sStatusCode;
00329 }
00330
00336 protected function _setResource( $rCurl )
00337 {
00338 $this->_rCurl = $rCurl;
00339 }
00340
00346 protected function _getResource()
00347 {
00348 if ( is_null( $this->_rCurl ) ) {
00349 $this->_setResource( curl_init() );
00350 }
00351 return $this->_rCurl;
00352 }
00353
00357 protected function _setOptions()
00358 {
00359 if (!is_null($this->getHeader())) {
00360 $this->_setOpt( CURLOPT_HTTPHEADER, $this->getHeader() );
00361 }
00362 $this->_setOpt( CURLOPT_URL, $this->getUrl() );
00363
00364 if ( $this->getMethod() == "POST" ) {
00365 $this->_setOpt( CURLOPT_POST, 1 );
00366 $this->_setOpt( CURLOPT_POSTFIELDS, $this->getQuery() );
00367 }
00368
00369 $aOptions = $this->getOptions();
00370 if ( count($aOptions) ) {
00371 foreach( $aOptions as $sName => $mValue ) {
00372 $this->_setOpt( constant( $sName ), $mValue );
00373 }
00374 }
00375 }
00376
00382 protected function _execute()
00383 {
00384 return curl_exec( $this->_getResource() );
00385 }
00386
00392 protected function _close()
00393 {
00394 curl_close( $this->_getResource() );
00395 $this->_setResource( null );
00396 }
00397
00404 protected function _setOpt( $sName, $sValue )
00405 {
00406 curl_setopt( $this->_getResource(), $sName, $sValue );
00407 }
00408
00414 protected function _getErrorNumber()
00415 {
00416 return curl_errno( $this->_getResource() );
00417 }
00418
00422 protected function _saveStatusCode()
00423 {
00424 $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
00425 }
00426
00433 protected function _prepareQueryParameters( $aParams )
00434 {
00435 $aParams = array_filter( $aParams );
00436 $aParams = array_map( array( $this, '_htmlDecode' ), $aParams );
00437
00438 return $aParams;
00439 }
00440
00448 protected function _htmlDecode( $mParam )
00449 {
00450 if ( is_array( $mParam ) ) {
00451 $mParam = $this->_prepareQueryParameters( $mParam );
00452 } else {
00453 $mParam = html_entity_decode( stripslashes( $mParam ), ENT_QUOTES, $this->getConnectionCharset() );
00454 }
00455
00456 return $mParam;
00457 }
00458
00459 }