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 $_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  )
00109     {
00110         $this->_sQuery = $sQuery;
00111     }
00112 
00118     public function getQuery()
00119     {
00120         if ( is_null( $this->_sQuery ) ) {
00121             $sQuery = "";
00122             if ( $aParams = $this->getParameters() ) {
00123                 $aParams = $this->_prepareQueryParameters( $aParams );
00124                 $sQuery = http_build_query( $aParams, "", "&" );
00125             }
00126             $this->setQuery($sQuery);
00127         }
00128 
00129         return $this->_sQuery;
00130     }
00131 
00137     public function setParameters( $aParameters )
00138     {
00139         $this->setQuery( null );
00140         $this->_aParameters = $aParameters;
00141     }
00142 
00148     public function getParameters()
00149     {
00150         return $this->_aParameters;
00151     }
00152 
00160     public function setHost( $sHost )
00161     {
00162         $this->_sHost = $sHost;
00163     }
00164 
00170     public function getHost()
00171     {
00172         return $this->_sHost;
00173     }
00174 
00182     public function setHeader( $aHeader = null )
00183     {
00184         if ( is_null( $aHeader ) && $this->getMethod() == "POST") {
00185             $sHost = $this->getHost();
00186 
00187             $aHeader = array();
00188             $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00189             $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00190             if ( isset( $sHost ) ) {
00191                 $aHeader[] = 'Host: '. $sHost;
00192             }
00193             $aHeader[] = 'Connection: close';
00194         }
00195         $this->_aHeader = $aHeader;
00196     }
00197 
00203     public function getHeader()
00204     {
00205         if ( is_null( $this->_aHeader ) ) {
00206             $this->setHeader();
00207         }
00208         return $this->_aHeader;
00209     }
00210 
00218     public function setMethod($sMethod)
00219     {
00220         $this->_sMethod = strtoupper($sMethod);
00221     }
00222 
00228     public function getMethod()
00229     {
00230         return $this->_sMethod;
00231     }
00232 
00243     public function setOption( $sName, $sValue )
00244     {
00245         if (strpos( $sName, 'CURLOPT_' ) !== 0 || !defined($sConstant = strtoupper($sName))) {
00249             $oException = oxNew( 'oxException' );
00250             $oLang = oxRegistry::getLang();
00251             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage() ), $sName ) );
00252             throw $oException;
00253         }
00254 
00255         $this->_aOptions[$sName] = $sValue;
00256     }
00257 
00263     public function getOptions()
00264     {
00265         return $this->_aOptions;
00266     }
00267 
00275     public function execute()
00276     {
00277         $this->_setOptions();
00278 
00279         $sResponse = $this->_execute();
00280         $this->_saveStatusCode();
00281 
00282         $iCurlErrorNumber = $this->_getErrorNumber();
00283 
00284         $this->_close();
00285 
00286         if ( $iCurlErrorNumber ) {
00290             $oException = oxNew( 'oxException' );
00291             $oLang = oxRegistry::getLang();
00292             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_CURL_ERROR', $oLang->getTplLanguage() ), $iCurlErrorNumber ) );
00293             throw $oException;
00294         }
00295 
00296         return $sResponse;
00297     }
00298 
00304     public function setConnectionCharset( $sCharset )
00305     {
00306         $this->_sConnectionCharset = $sCharset;
00307     }
00308 
00314     public function getConnectionCharset()
00315     {
00316         return $this->_sConnectionCharset;
00317     }
00318 
00324     public function getStatusCode()
00325     {
00326         return $this->_sStatusCode;
00327     }
00328 
00334     protected function _setResource( $rCurl )
00335     {
00336         $this->_rCurl = $rCurl;
00337     }
00338 
00344     protected function _getResource()
00345     {
00346         if ( is_null( $this->_rCurl ) ) {
00347             $this->_setResource( curl_init() );
00348         }
00349         return $this->_rCurl;
00350     }
00351 
00355     protected function _setOptions()
00356     {
00357         if (!is_null($this->getHeader())) {
00358             $this->_setOpt( CURLOPT_HTTPHEADER, $this->getHeader() );
00359         }
00360         $this->_setOpt( CURLOPT_URL, $this->getUrl() );
00361 
00362         if ( $this->getMethod() == "POST" ) {
00363             $this->_setOpt( CURLOPT_POST, 1 );
00364             $this->_setOpt( CURLOPT_POSTFIELDS, $this->getQuery() );
00365         }
00366 
00367         $aOptions = $this->getOptions();
00368         if ( count($aOptions) ) {
00369             foreach( $aOptions as $sName => $mValue  ) {
00370                 $this->_setOpt( constant( $sName ), $mValue );
00371             }
00372         }
00373     }
00374 
00380     protected function _execute()
00381     {
00382         return curl_exec( $this->_getResource() );
00383     }
00384 
00390     protected function _close()
00391     {
00392         curl_close( $this->_getResource() );
00393         $this->_setResource( null );
00394     }
00395 
00402     protected function _setOpt( $sName, $sValue )
00403     {
00404         curl_setopt( $this->_getResource(), $sName, $sValue );
00405     }
00406 
00412     protected function _getErrorNumber()
00413     {
00414         return curl_errno( $this->_getResource() );
00415     }
00416 
00420     protected function _saveStatusCode()
00421     {
00422         $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
00423     }
00424 
00431     protected function _prepareQueryParameters( $aParams )
00432     {
00433         $aParams = array_filter( $aParams );
00434         $aParams = array_map( array( $this, '_htmlDecode' ), $aParams );
00435 
00436         return $aParams;
00437     }
00438 
00446     protected function _htmlDecode( $mParam )
00447     {
00448         if ( is_array( $mParam ) ) {
00449             $mParam = $this->_prepareQueryParameters( $mParam );
00450         } else {
00451             $mParam = html_entity_decode( stripslashes( $mParam ), ENT_QUOTES, $this->getConnectionCharset() );
00452         }
00453 
00454         return $mParam;
00455     }
00456 
00457 }