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 
00071 
00081     public function setUrl( $sUrl )
00082     {
00083         $this->_sUrl = $sUrl;
00084     }
00085 
00091     public function getUrl()
00092     {
00093         if ($this->getMethod() == "GET" && $this->getQuery()) {
00094             $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
00095         }
00096         return $this->_sUrl;
00097     }
00098 
00102     public function setQuery( $sQuery = null )
00103     {
00104         if ( is_null($sQuery) ) {
00105             $sQuery = "";
00106             if ( $aParams = $this->getParameters() ) {
00107                 $aParams = $this->_prepareQueryParameters( $aParams );
00108                 $sQuery = http_build_query( $aParams, "", "&" );
00109             }
00110         }
00111 
00112         $this->_sQuery = $sQuery;
00113     }
00114 
00120     public function getQuery()
00121     {
00122         if ( is_null( $this->_sQuery ) ) {
00123             $this->setQuery();
00124         }
00125 
00126         return $this->_sQuery;
00127     }
00128 
00134     public function setParameters( $aParameters )
00135     {
00136         $this->_aParameters = $aParameters;
00137     }
00138 
00144     public function getParameters()
00145     {
00146         return $this->_aParameters;
00147     }
00148 
00156     public function setHost( $sHost )
00157     {
00158         $this->_sHost = $sHost;
00159     }
00160 
00166     public function getHost()
00167     {
00168         return $this->_sHost;
00169     }
00170 
00178     public function setHeader( $aHeader = null )
00179     {
00180         if ( is_null( $aHeader ) && $this->getMethod() == "POST") {
00181             $sHost = $this->getHost();
00182 
00183             $aHeader = array();
00184             $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00185             $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00186             if ( isset( $sHost ) ) {
00187                 $aHeader[] = 'Host: '. $sHost;
00188             }
00189             $aHeader[] = 'Connection: close';
00190         }
00191         $this->_aHeader = $aHeader;
00192     }
00193 
00199     public function getHeader()
00200     {
00201         if ( is_null( $this->_aHeader ) ) {
00202             $this->setHeader();
00203         }
00204         return $this->_aHeader;
00205     }
00206 
00214     public function setMethod($sMethod)
00215     {
00216         $this->_sMethod = strtoupper($sMethod);
00217     }
00218 
00224     public function getMethod()
00225     {
00226         return $this->_sMethod;
00227     }
00228 
00239     public function setOption( $sName, $sValue )
00240     {
00241         if (strpos( $sName, 'CURLOPT_' ) !== 0 || !defined($sConstant = strtoupper($sName))) {
00245             $oException = oxNew( 'oxException' );
00246             $oLang = oxRegistry::getLang();
00247             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage() ), $sName ) );
00248             throw $oException;
00249         }
00250 
00251         $this->_aOptions[$sName] = $sValue;
00252     }
00253 
00259     public function getOptions()
00260     {
00261         return $this->_aOptions;
00262     }
00263 
00271     public function execute()
00272     {
00273         $this->_setOptions();
00274 
00275         $sResponse = $this->_execute();
00276         $iCurlErrorNumber = $this->_getErrorNumber();
00277 
00278         $this->_close();
00279 
00280         if ( $iCurlErrorNumber ) {
00284             $oException = oxNew( 'oxException' );
00285             $oLang = oxRegistry::getLang();
00286             $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_CURL_ERROR', $oLang->getTplLanguage() ), $iCurlErrorNumber ) );
00287             throw $oException;
00288         }
00289 
00290         return $sResponse;
00291     }
00292 
00298     public function setConnectionCharset( $sCharset )
00299     {
00300         $this->_sConnectionCharset = $sCharset;
00301     }
00302 
00308     public function getConnectionCharset()
00309     {
00310         return $this->_sConnectionCharset;
00311     }
00312 
00318     protected function _setResource( $rCurl )
00319     {
00320         $this->_rCurl = $rCurl;
00321     }
00322 
00328     protected function _getResource()
00329     {
00330         if ( is_null( $this->_rCurl ) ) {
00331             $this->_setResource( curl_init() );
00332         }
00333         return $this->_rCurl;
00334     }
00335 
00339     protected function _setOptions()
00340     {
00341         if (!is_null($this->getHeader())) {
00342             $this->_setOpt( CURLOPT_HTTPHEADER, $this->getHeader() );
00343         }
00344         $this->_setOpt( CURLOPT_URL, $this->getUrl() );
00345 
00346         if ( $this->getMethod() == "POST" ) {
00347             $this->_setOpt( CURLOPT_POST, 1 );
00348             $this->_setOpt( CURLOPT_POSTFIELDS, $this->getQuery() );
00349         }
00350 
00351         $aOptions = $this->getOptions();
00352         if ( count($aOptions) ) {
00353             foreach( $aOptions as $sName => $mValue  ) {
00354                 $this->_setOpt( constant( $sName ), $mValue );
00355             }
00356         }
00357     }
00358 
00364     protected function _execute()
00365     {
00366         return curl_exec( $this->_getResource() );
00367     }
00368 
00374     protected function _close()
00375     {
00376         curl_close( $this->_getResource() );
00377         $this->_setResource( null );
00378     }
00379 
00386     protected function _setOpt( $sName, $sValue )
00387     {
00388         curl_setopt( $this->_getResource(), $sName, $sValue );
00389     }
00390 
00396     protected function _getErrorNumber()
00397     {
00398         return curl_errno( $this->_getResource() );
00399     }
00400 
00407     protected function _prepareQueryParameters( $aParams )
00408     {
00409         $aParams = array_filter( $aParams );
00410         $aParams = array_map( array( $this, '_htmlDecode' ), $aParams );
00411 
00412         return $aParams;
00413     }
00414 
00422     protected function _htmlDecode( $mParam )
00423     {
00424         if ( is_array( $mParam ) ) {
00425             $mParam = $this->_prepareQueryParameters( $mParam );
00426         } else {
00427             $mParam = html_entity_decode( stripslashes( $mParam ), ENT_QUOTES, $this->getConnectionCharset() );
00428         }
00429 
00430         return $mParam;
00431     }
00432 
00433 }