OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxcurl.php
Go to the documentation of this file.
1 <?php
2 
9 class oxCurl
10 {
16  protected $_rCurl = null;
17 
23  protected $_sUrl = null;
24 
30  protected $_sQuery = null;
31 
37  protected $_sMethod = 'POST';
38 
44  protected $_aParameters = null;
45 
50  protected $_sConnectionCharset = "UTF-8";
51 
56  protected $_aHeader = null;
57 
62  protected $_sHost = null;
63 
69  protected $_aOptions = array('CURLOPT_RETURNTRANSFER' => 1);
70 
76  protected $_sStatusCode = null;
77 
87  public function setUrl( $sUrl )
88  {
89  $this->_sUrl = $sUrl;
90  }
91 
97  public function getUrl()
98  {
99  if ($this->getMethod() == "GET" && $this->getQuery()) {
100  $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
101  }
102  return $this->_sUrl;
103  }
104 
108  public function setQuery( $sQuery )
109  {
110  $this->_sQuery = $sQuery;
111  }
112 
118  public function getQuery()
119  {
120  if ( is_null( $this->_sQuery ) ) {
121  $sQuery = "";
122  if ( $aParams = $this->getParameters() ) {
123  $aParams = $this->_prepareQueryParameters( $aParams );
124  $sQuery = http_build_query( $aParams, "", "&" );
125  }
126  $this->setQuery($sQuery);
127  }
128 
129  return $this->_sQuery;
130  }
131 
137  public function setParameters( $aParameters )
138  {
139  $this->setQuery( null );
140  $this->_aParameters = $aParameters;
141  }
142 
148  public function getParameters()
149  {
150  return $this->_aParameters;
151  }
152 
160  public function setHost( $sHost )
161  {
162  $this->_sHost = $sHost;
163  }
164 
170  public function getHost()
171  {
172  return $this->_sHost;
173  }
174 
182  public function setHeader( $aHeader = null )
183  {
184  if ( is_null( $aHeader ) && $this->getMethod() == "POST") {
185  $sHost = $this->getHost();
186 
187  $aHeader = array();
188  $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
189  $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
190  if ( isset( $sHost ) ) {
191  $aHeader[] = 'Host: '. $sHost;
192  }
193  $aHeader[] = 'Connection: close';
194  }
195  $this->_aHeader = $aHeader;
196  }
197 
203  public function getHeader()
204  {
205  if ( is_null( $this->_aHeader ) ) {
206  $this->setHeader();
207  }
208  return $this->_aHeader;
209  }
210 
218  public function setMethod($sMethod)
219  {
220  $this->_sMethod = strtoupper($sMethod);
221  }
222 
228  public function getMethod()
229  {
230  return $this->_sMethod;
231  }
232 
243  public function setOption( $sName, $sValue )
244  {
245  if (strpos( $sName, 'CURLOPT_' ) !== 0 || !defined($sConstant = strtoupper($sName))) {
249  $oException = oxNew( 'oxException' );
250  $oLang = oxRegistry::getLang();
251  $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_NOT_VALID_CURL_CONSTANT', $oLang->getTplLanguage() ), $sName ) );
252  throw $oException;
253  }
254 
255  $this->_aOptions[$sName] = $sValue;
256  }
257 
263  public function getOptions()
264  {
265  return $this->_aOptions;
266  }
267 
275  public function execute()
276  {
277  $this->_setOptions();
278 
279  $sResponse = $this->_execute();
280  $this->_saveStatusCode();
281 
282  $iCurlErrorNumber = $this->_getErrorNumber();
283 
284  $this->_close();
285 
286  if ( $iCurlErrorNumber ) {
290  $oException = oxNew( 'oxException' );
291  $oLang = oxRegistry::getLang();
292  $oException->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_CURL_ERROR', $oLang->getTplLanguage() ), $iCurlErrorNumber ) );
293  throw $oException;
294  }
295 
296  return $sResponse;
297  }
298 
304  public function setConnectionCharset( $sCharset )
305  {
306  $this->_sConnectionCharset = $sCharset;
307  }
308 
314  public function getConnectionCharset()
315  {
317  }
318 
324  public function getStatusCode()
325  {
326  return $this->_sStatusCode;
327  }
328 
334  protected function _setResource( $rCurl )
335  {
336  $this->_rCurl = $rCurl;
337  }
338 
344  protected function _getResource()
345  {
346  if ( is_null( $this->_rCurl ) ) {
347  $this->_setResource( curl_init() );
348  }
349  return $this->_rCurl;
350  }
351 
355  protected function _setOptions()
356  {
357  if (!is_null($this->getHeader())) {
358  $this->_setOpt( CURLOPT_HTTPHEADER, $this->getHeader() );
359  }
360  $this->_setOpt( CURLOPT_URL, $this->getUrl() );
361 
362  if ( $this->getMethod() == "POST" ) {
363  $this->_setOpt( CURLOPT_POST, 1 );
364  $this->_setOpt( CURLOPT_POSTFIELDS, $this->getQuery() );
365  }
366 
367  $aOptions = $this->getOptions();
368  if ( count($aOptions) ) {
369  foreach( $aOptions as $sName => $mValue ) {
370  $this->_setOpt( constant( $sName ), $mValue );
371  }
372  }
373  }
374 
380  protected function _execute()
381  {
382  return curl_exec( $this->_getResource() );
383  }
384 
390  protected function _close()
391  {
392  curl_close( $this->_getResource() );
393  $this->_setResource( null );
394  }
395 
402  protected function _setOpt( $sName, $sValue )
403  {
404  curl_setopt( $this->_getResource(), $sName, $sValue );
405  }
406 
412  protected function _getErrorNumber()
413  {
414  return curl_errno( $this->_getResource() );
415  }
416 
420  protected function _saveStatusCode()
421  {
422  $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
423  }
424 
431  protected function _prepareQueryParameters( $aParams )
432  {
433  $aParams = array_filter( $aParams );
434  $aParams = array_map( array( $this, '_htmlDecode' ), $aParams );
435 
436  return $aParams;
437  }
438 
446  protected function _htmlDecode( $mParam )
447  {
448  if ( is_array( $mParam ) ) {
449  $mParam = $this->_prepareQueryParameters( $mParam );
450  } else {
451  $mParam = html_entity_decode( stripslashes( $mParam ), ENT_QUOTES, $this->getConnectionCharset() );
452  }
453 
454  return $mParam;
455  }
456 
457 }