oxcurl.php

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 function _setResource( $rCurl )
00077     {
00078         $this->_rCurl = $rCurl;
00079     }
00080 
00086     protected function _getResource()
00087     {
00088         if ( is_null( $this->_rCurl ) ) {
00089             $this->_setResource( curl_init() );
00090         }
00091         return $this->_rCurl;
00092     }
00093 
00103     public function setUrl( $sUrl )
00104     {
00105         $this->_sUrl = $sUrl;
00106     }
00107 
00113     public function getUrl()
00114     {
00115         if ($this->getMethod() == "GET" && $this->getQuery()) {
00116             $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
00117         }
00118         return $this->_sUrl;
00119     }
00120 
00124     public function setQuery( $sQuery = null )
00125     {
00126         if ( is_null($sQuery) ) {
00127             $sQuery = "";
00128             if ($aParams = $this->getParameters()) {
00129                 $aParams = array_filter( $aParams );
00130                 $aParams = array_map(array($this, '_htmlDecode'), $aParams);
00131 
00132                 $sQuery = http_build_query( $aParams, "", "&" );
00133             }
00134         }
00135 
00136         $this->_sQuery = $sQuery;
00137     }
00138 
00144     public function getQuery()
00145     {
00146         if ( is_null( $this->_sQuery ) ) {
00147             $this->setQuery();
00148         }
00149 
00150         return $this->_sQuery;
00151     }
00152 
00158     public function setParameters( $aParameters )
00159     {
00160         $this->_aParameters = $aParameters;
00161     }
00162 
00168     public function getParameters()
00169     {
00170         return $this->_aParameters;
00171     }
00172 
00180     public function setHost( $sHost )
00181     {
00182         $this->_sHost = $sHost;
00183     }
00184 
00190     public function getHost()
00191     {
00192         return $this->_sHost;
00193     }
00194 
00202     public function setHeader( $aHeader = null )
00203     {
00204         if ( is_null( $aHeader ) && $this->getMethod() == "POST") {
00205             $sHost = $this->getHost();
00206 
00207             $aHeader = array();
00208             $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00209             $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00210             if ( isset( $sHost ) ) {
00211                 $aHeader[] = 'Host: '. $sHost;
00212             }
00213             $aHeader[] = 'Connection: close';
00214         }
00215         $this->_aHeader = $aHeader;
00216     }
00217 
00223     public function getHeader()
00224     {
00225         if ( is_null( $this->_aHeader ) ) {
00226             $this->setHeader();
00227         }
00228         return $this->_aHeader;
00229     }
00230 
00238     public function setMethod($sMethod)
00239     {
00240         $this->_sMethod = strtoupper($sMethod);
00241     }
00242 
00248     public function getMethod()
00249     {
00250         return $this->_sMethod;
00251     }
00252 
00263     public function setOption( $sName, $sValue )
00264     {
00265         if (strpos( $sName, 'CURLOPT_' ) !== 0 || !defined($sConstant = strtoupper($sName))) {
00269             $oException = oxNew( 'oxException' );
00270             $oLang = oxRegistry::getLang();
00271             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage() ), $sName ) );
00272             throw $oException;
00273         }
00274 
00275         $this->_aOptions[$sName] = $sValue;
00276     }
00277 
00283     public function getOptions()
00284     {
00285         return $this->_aOptions;
00286     }
00287 
00295     public function execute()
00296     {
00297         $this->_setOptions();
00298 
00299         $sResponse = $this->_execute();
00300         $iCurlErrorNumber = $this->_getErrorNumber();
00301 
00302         $this->_close();
00303 
00304         if ( $iCurlErrorNumber ) {
00308             $oException = oxNew( 'oxException' );
00309             $oLang = oxRegistry::getLang();
00310             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_CURL_ERROR', $oLang->getTplLanguage() ), $iCurlErrorNumber ) );
00311             throw $oException;
00312         }
00313 
00314         return $sResponse;
00315     }
00316 
00320     protected function _setOptions()
00321     {
00322         if (!is_null($this->getHeader())) {
00323             $this->_setOpt( CURLOPT_HTTPHEADER, $this->getHeader() );
00324         }
00325         $this->_setOpt( CURLOPT_URL, $this->getUrl() );
00326 
00327         if ( $this->getMethod() == "POST" ) {
00328             $this->_setOpt( CURLOPT_POST, 1 );
00329             $this->_setOpt( CURLOPT_POSTFIELDS, $this->getQuery() );
00330         }
00331 
00332         $aOptions = $this->getOptions();
00333         if ( count($aOptions) ) {
00334             foreach( $aOptions as $sName => $mValue  ) {
00335                 $this->_setOpt( constant( $sName ), $mValue );
00336             }
00337         }
00338     }
00339 
00345     protected function _execute()
00346     {
00347         return curl_exec( $this->_getResource() );
00348     }
00349 
00355     protected function _close()
00356     {
00357         curl_close( $this->_getResource() );
00358         $this->_setResource( null );
00359     }
00360 
00367     protected function _setOpt( $sName, $sValue )
00368     {
00369         curl_setopt( $this->_getResource(), $sName, $sValue );
00370     }
00371 
00377     protected function _getErrorNumber()
00378     {
00379         return curl_errno( $this->_getResource() );
00380     }
00381 
00389     protected function _htmlDecode( $sString )
00390     {
00391         $sString = html_entity_decode( stripslashes( $sString ), ENT_QUOTES, $this->getConnectionCharset() );
00392 
00393         return $sString;
00394     }
00395 
00401     public function setConnectionCharset( $sCharset )
00402     {
00403         $this->_sConnectionCharset = $sCharset;
00404     }
00405 
00411     public function getConnectionCharset()
00412     {
00413         return $this->_sConnectionCharset;
00414     }
00415 
00416 }