OXID eShop CE  4.10.0
 All Classes Namespaces Files Functions Variables Pages
oxcurl.php
Go to the documentation of this file.
1 <?php
2 
9 class oxCurl
10 {
12  const EXECUTION_TIMEOUT_OPTION = 'CURLOPT_TIMEOUT';
13 
19  protected $_rCurl = null;
20 
26  protected $_sUrl = null;
27 
33  protected $_sQuery = null;
34 
40  protected $_sMethod = 'POST';
41 
47  protected $_aParameters = null;
48 
54  protected $_sConnectionCharset = "UTF-8";
55 
61  protected $_aHeader = null;
62 
68  protected $_sHost = null;
69 
75  protected $_aOptions = array('CURLOPT_RETURNTRANSFER' => 1);
76 
82  protected $_sStatusCode = null;
83 
91  public function setUrl($sUrl)
92  {
93  $this->_sUrl = $sUrl;
94  }
95 
101  public function getUrl()
102  {
103  if ($this->getMethod() == "GET" && $this->getQuery()) {
104  $this->_sUrl = $this->_sUrl . "?" . $this->getQuery();
105  }
106 
107  return $this->_sUrl;
108  }
109 
115  public function setQuery($sQuery)
116  {
117  $this->_sQuery = $sQuery;
118  }
119 
125  public function getQuery()
126  {
127  if (is_null($this->_sQuery)) {
128  $sQuery = "";
129  if ($aParams = $this->getParameters()) {
130  $aParams = $this->_prepareQueryParameters($aParams);
131  $sQuery = http_build_query($aParams, "", "&");
132  }
133  $this->setQuery($sQuery);
134  }
135 
136  return $this->_sQuery;
137  }
138 
144  public function setParameters($aParameters)
145  {
146  $this->setQuery(null);
147  $this->_aParameters = $aParameters;
148  }
149 
155  public function getParameters()
156  {
157  return $this->_aParameters;
158  }
159 
165  public function setHost($sHost)
166  {
167  $this->_sHost = $sHost;
168  }
169 
175  public function getHost()
176  {
177  return $this->_sHost;
178  }
179 
185  public function setHeader($aHeader = null)
186  {
187  if (is_null($aHeader) && $this->getMethod() == "POST") {
188  $sHost = $this->getHost();
189 
190  $aHeader = array();
191  $aHeader[] = 'POST /cgi-bin/webscr HTTP/1.1';
192  $aHeader[] = 'Content-Type: application/x-www-form-urlencoded';
193  if (isset($sHost)) {
194  $aHeader[] = 'Host: ' . $sHost;
195  }
196  $aHeader[] = 'Connection: close';
197  }
198  $this->_aHeader = $aHeader;
199  }
200 
206  public function getHeader()
207  {
208  if (is_null($this->_aHeader)) {
209  $this->setHeader();
210  }
211 
212  return $this->_aHeader;
213  }
214 
220  public function setMethod($sMethod)
221  {
222  $this->_sMethod = strtoupper($sMethod);
223  }
224 
230  public function getMethod()
231  {
232  return $this->_sMethod;
233  }
234 
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 
350  return $this->_rCurl;
351  }
352 
356  protected function _setOptions()
357  {
358  if (!is_null($this->getHeader())) {
359  $this->_setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
360  }
361  $this->_setOpt(CURLOPT_URL, $this->getUrl());
362 
363  if ($this->getMethod() == "POST") {
364  $this->_setOpt(CURLOPT_POST, 1);
365  $this->_setOpt(CURLOPT_POSTFIELDS, $this->getQuery());
366  }
367 
368  $aOptions = $this->getOptions();
369  if (count($aOptions)) {
370  foreach ($aOptions as $sName => $mValue) {
371  $this->_setOpt(constant($sName), $mValue);
372  }
373  }
374  }
375 
381  protected function _execute()
382  {
383  return curl_exec($this->_getResource());
384  }
385 
389  protected function _close()
390  {
391  curl_close($this->_getResource());
392  $this->_setResource(null);
393  }
394 
401  protected function _setOpt($sName, $sValue)
402  {
403  curl_setopt($this->_getResource(), $sName, $sValue);
404  }
405 
411  protected function _getErrorNumber()
412  {
413  return curl_errno($this->_getResource());
414  }
415 
419  protected function _saveStatusCode()
420  {
421  $this->_sStatusCode = curl_getinfo($this->_getResource(), CURLINFO_HTTP_CODE);
422  }
423 
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 }