oxcurl.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxCurl
00010 {
00011 
00017     protected $_rCurl = null;
00018 
00024     protected $_sUrl = null;
00025 
00031     protected $_sQuery = null;
00032 
00038     protected $_sMethod = 'POST';
00039 
00045     protected $_aParameters = null;
00046 
00052     protected $_sConnectionCharset = "UTF-8";
00053 
00059     protected $_aHeader = null;
00060 
00066     protected $_sHost = null;
00067 
00073     protected $_aOptions = array('CURLOPT_RETURNTRANSFER' => 1);
00074 
00080     protected $_sStatusCode = null;
00081 
00089     public function setUrl($sUrl)
00090     {
00091         $this->_sUrl = $sUrl;
00092     }
00093 
00099     public function getUrl()
00100     {
00101         if ($this->getMethod() == "GET" && $this->getQuery()) {
00102             $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
00103         }
00104 
00105         return $this->_sUrl;
00106     }
00107 
00113     public function setQuery($sQuery)
00114     {
00115         $this->_sQuery = $sQuery;
00116     }
00117 
00123     public function getQuery()
00124     {
00125         if (is_null($this->_sQuery)) {
00126             $sQuery = "";
00127             if ($aParams = $this->getParameters()) {
00128                 $aParams = $this->_prepareQueryParameters($aParams);
00129                 $sQuery = http_build_query($aParams, "", "&");
00130             }
00131             $this->setQuery($sQuery);
00132         }
00133 
00134         return $this->_sQuery;
00135     }
00136 
00142     public function setParameters($aParameters)
00143     {
00144         $this->setQuery(null);
00145         $this->_aParameters = $aParameters;
00146     }
00147 
00153     public function getParameters()
00154     {
00155         return $this->_aParameters;
00156     }
00157 
00163     public function setHost($sHost)
00164     {
00165         $this->_sHost = $sHost;
00166     }
00167 
00173     public function getHost()
00174     {
00175         return $this->_sHost;
00176     }
00177 
00183     public function setHeader($aHeader = null)
00184     {
00185         if (is_null($aHeader) && $this->getMethod() == "POST") {
00186             $sHost = $this->getHost();
00187 
00188             $aHeader = array();
00189             $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
00190             $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
00191             if (isset($sHost)) {
00192                 $aHeader[] = 'Host: ' . $sHost;
00193             }
00194             $aHeader[] = 'Connection: close';
00195         }
00196         $this->_aHeader = $aHeader;
00197     }
00198 
00204     public function getHeader()
00205     {
00206         if (is_null($this->_aHeader)) {
00207             $this->setHeader();
00208         }
00209 
00210         return $this->_aHeader;
00211     }
00212 
00218     public function setMethod($sMethod)
00219     {
00220         $this->_sMethod = strtoupper($sMethod);
00221     }
00222 
00228     public function getMethod()
00229     {
00230         return $this->_sMethod;
00231     }
00232 
00241     public function setOption($sName, $sValue)
00242     {
00243         if (strpos($sName, 'CURLOPT_') !== 0 || !defined($sConstant = strtoupper($sName))) {
00247             $oException = oxNew('oxException');
00248             $oLang = oxRegistry::getLang();
00249             $oException->setMessage(sprintf($oLang->translateString('EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage()), $sName));
00250             throw $oException;
00251         }
00252 
00253         $this->_aOptions[$sName] = $sValue;
00254     }
00255 
00261     public function getOptions()
00262     {
00263         return $this->_aOptions;
00264     }
00265 
00273     public function execute()
00274     {
00275         $this->_setOptions();
00276 
00277         $sResponse = $this->_execute();
00278         $this->_saveStatusCode();
00279 
00280         $iCurlErrorNumber = $this->_getErrorNumber();
00281 
00282         $this->_close();
00283 
00284         if ($iCurlErrorNumber) {
00288             $oException = oxNew('oxException');
00289             $oLang = oxRegistry::getLang();
00290             $oException->setMessage(sprintf($oLang->translateString('EXCEPTION_CURL_ERROR', $oLang->getTplLanguage()), $iCurlErrorNumber));
00291             throw $oException;
00292         }
00293 
00294         return $sResponse;
00295     }
00296 
00302     public function setConnectionCharset($sCharset)
00303     {
00304         $this->_sConnectionCharset = $sCharset;
00305     }
00306 
00312     public function getConnectionCharset()
00313     {
00314         return $this->_sConnectionCharset;
00315     }
00316 
00322     public function getStatusCode()
00323     {
00324         return $this->_sStatusCode;
00325     }
00326 
00332     protected function _setResource($rCurl)
00333     {
00334         $this->_rCurl = $rCurl;
00335     }
00336 
00342     protected function _getResource()
00343     {
00344         if (is_null($this->_rCurl)) {
00345             $this->_setResource(curl_init());
00346         }
00347 
00348         return $this->_rCurl;
00349     }
00350 
00354     protected function _setOptions()
00355     {
00356         if (!is_null($this->getHeader())) {
00357             $this->_setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
00358         }
00359         $this->_setOpt(CURLOPT_URL, $this->getUrl());
00360 
00361         if ($this->getMethod() == "POST") {
00362             $this->_setOpt(CURLOPT_POST, 1);
00363             $this->_setOpt(CURLOPT_POSTFIELDS, $this->getQuery());
00364         }
00365 
00366         $aOptions = $this->getOptions();
00367         if (count($aOptions)) {
00368             foreach ($aOptions as $sName => $mValue) {
00369                 $this->_setOpt(constant($sName), $mValue);
00370             }
00371         }
00372     }
00373 
00379     protected function _execute()
00380     {
00381         return curl_exec($this->_getResource());
00382     }
00383 
00387     protected function _close()
00388     {
00389         curl_close($this->_getResource());
00390         $this->_setResource(null);
00391     }
00392 
00399     protected function _setOpt($sName, $sValue)
00400     {
00401         curl_setopt($this->_getResource(), $sName, $sValue);
00402     }
00403 
00409     protected function _getErrorNumber()
00410     {
00411         return curl_errno($this->_getResource());
00412     }
00413 
00417     protected function _saveStatusCode()
00418     {
00419         $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
00420     }
00421 
00429     protected function _prepareQueryParameters($aParams)
00430     {
00431         $aParams = array_filter($aParams);
00432         $aParams = array_map(array($this, '_htmlDecode'), $aParams);
00433 
00434         return $aParams;
00435     }
00436 
00444     protected function _htmlDecode($mParam)
00445     {
00446         if (is_array($mParam)) {
00447             $mParam = $this->_prepareQueryParameters($mParam);
00448         } else {
00449             $mParam = html_entity_decode(stripslashes($mParam), ENT_QUOTES, $this->getConnectionCharset());
00450         }
00451 
00452         return $mParam;
00453     }
00454 }