oxcurl.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxCurl
00010 {
00012     const EXECUTION_TIMEOUT_OPTION = 'CURLOPT_TIMEOUT';
00013 
00019     protected $_rCurl = null;
00020 
00026     protected $_sUrl = null;
00027 
00033     protected $_sQuery = null;
00034 
00040     protected $_sMethod = 'POST';
00041 
00047     protected $_aParameters = null;
00048 
00054     protected $_sConnectionCharset = "UTF-8";
00055 
00061     protected $_aHeader = null;
00062 
00068     protected $_sHost = null;
00069 
00075     protected $_aOptions = array('CURLOPT_RETURNTRANSFER' => 1);
00076 
00082     protected $_sStatusCode = null;
00083 
00091     public function setUrl($sUrl)
00092     {
00093         $this->_sUrl = $sUrl;
00094     }
00095 
00101     public function getUrl()
00102     {
00103         if ($this->getMethod() == "GET" && $this->getQuery()) {
00104             $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
00105         }
00106 
00107         return $this->_sUrl;
00108     }
00109 
00115     public function setQuery($sQuery)
00116     {
00117         $this->_sQuery = $sQuery;
00118     }
00119 
00125     public function getQuery()
00126     {
00127         if (is_null($this->_sQuery)) {
00128             $sQuery = "";
00129             if ($aParams = $this->getParameters()) {
00130                 $aParams = $this->_prepareQueryParameters($aParams);
00131                 $sQuery = http_build_query($aParams, "", "&");
00132             }
00133             $this->setQuery($sQuery);
00134         }
00135 
00136         return $this->_sQuery;
00137     }
00138 
00144     public function setParameters($aParameters)
00145     {
00146         $this->setQuery(null);
00147         $this->_aParameters = $aParameters;
00148     }
00149 
00155     public function getParameters()
00156     {
00157         return $this->_aParameters;
00158     }
00159 
00165     public function setHost($sHost)
00166     {
00167         $this->_sHost = $sHost;
00168     }
00169 
00175     public function getHost()
00176     {
00177         return $this->_sHost;
00178     }
00179 
00185     public function setHeader($aHeader = null)
00186     {
00187         if (is_null($aHeader) && $this->getMethod() == "POST") {
00188             $sHost = $this->getHost();
00189 
00190             $aHeader = array();
00191             $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00192             $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00193             if (isset($sHost)) {
00194                 $aHeader[] = 'Host: ' . $sHost;
00195             }
00196             $aHeader[] = 'Connection: close';
00197         }
00198         $this->_aHeader = $aHeader;
00199     }
00200 
00206     public function getHeader()
00207     {
00208         if (is_null($this->_aHeader)) {
00209             $this->setHeader();
00210         }
00211 
00212         return $this->_aHeader;
00213     }
00214 
00220     public function setMethod($sMethod)
00221     {
00222         $this->_sMethod = strtoupper($sMethod);
00223     }
00224 
00230     public function getMethod()
00231     {
00232         return $this->_sMethod;
00233     }
00234 
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 
00350         return $this->_rCurl;
00351     }
00352 
00356     protected function _setOptions()
00357     {
00358         if (!is_null($this->getHeader())) {
00359             $this->_setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
00360         }
00361         $this->_setOpt(CURLOPT_URL, $this->getUrl());
00362 
00363         if ($this->getMethod() == "POST") {
00364             $this->_setOpt(CURLOPT_POST, 1);
00365             $this->_setOpt(CURLOPT_POSTFIELDS, $this->getQuery());
00366         }
00367 
00368         $aOptions = $this->getOptions();
00369         if (count($aOptions)) {
00370             foreach ($aOptions as $sName => $mValue) {
00371                 $this->_setOpt(constant($sName), $mValue);
00372             }
00373         }
00374     }
00375 
00381     protected function _execute()
00382     {
00383         return curl_exec($this->_getResource());
00384     }
00385 
00389     protected function _close()
00390     {
00391         curl_close($this->_getResource());
00392         $this->_setResource(null);
00393     }
00394 
00401     protected function _setOpt($sName, $sValue)
00402     {
00403         curl_setopt($this->_getResource(), $sName, $sValue);
00404     }
00405 
00411     protected function _getErrorNumber()
00412     {
00413         return curl_errno($this->_getResource());
00414     }
00415 
00419     protected function _saveStatusCode()
00420     {
00421         $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
00422     }
00423 
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 }