tpayCurl

Curl class which helps with CURL handling.

Defined (1)

The class is defined in the following location(s).

/includes/lib/src/_class_tpay/curl.php  
  1. class Curl 
  2. /** 
  3. * Last executed cURL info 
  4. * @var array|null 
  5. */ 
  6. private static $curlInfo; 
  7. /** 
  8. * Last executed cURL error 
  9. * @var string 
  10. */ 
  11. private static $curlError = ''; 
  12.  
  13. /** 
  14. * Last executed cURL errno 
  15. * @var string 
  16. */ 
  17. private static $curlErrno = ''; 
  18.  
  19. /** 
  20. * Get last info 
  21. * @return mixed 
  22. */ 
  23. public static function getCurlLastInfo() 
  24. return static::$curlInfo; 
  25.  
  26. /** 
  27. * Execute cURL request 
  28. * @param string $url action url 
  29. * @param array $postData array with post variables 
  30. * @return mixed 
  31. * @throws TException 
  32. */ 
  33. public static function doCurlRequest($url, $postData = array()) 
  34. if (!function_exists('curl_init') || !function_exists('curl_exec')) { 
  35. throw new TException('cURL function not available'); 
  36. $ch = curl_init(); 
  37. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
  40. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); 
  41. curl_setopt($ch, CURLOPT_TIMEOUT, 35); 
  42. curl_setopt($ch, CURLOPT_VERBOSE, 1); 
  43. curl_setopt($ch, CURLOPT_POST, 1); 
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  46. curl_setopt($ch, CURLOPT_FAILONERROR, true); 
  47. curl_setopt($ch, CURLOPT_URL, $url); 
  48. $curlRes = curl_exec($ch); 
  49.  
  50. static::$curlInfo = curl_getinfo($ch); 
  51. static::$curlError = curl_error($ch); 
  52. static::$curlErrno = curl_errno($ch); 
  53.  
  54. static::checkResponse(); 
  55.  
  56. curl_close($ch); 
  57.  
  58. return $curlRes; 
  59.  
  60. /** 
  61. * List of http response codes the occurrence of which results in throw exception 
  62. * @var array 
  63. */ 
  64. private static $httpCodes = array( 
  65. 500 => '500: Internal Server Error',  
  66. 501 => '501: Not Implemented',  
  67. 502 => '502: Bad Gateway',  
  68. 503 => '503: Service Unavailable',  
  69. 504 => '504: Gateway Timeout',  
  70. 505 => '505: HTTP Version Not Supported',  
  71. ); 
  72.  
  73. /** 
  74. * Check cURL response and throw exception if code is not allowed 
  75. * @throws TException 
  76. */ 
  77. private static function checkResponse() 
  78. $responseCode = static::$curlInfo['http_code']; 
  79. if ($responseCode !== 200) { 
  80. if (isset(static::$httpCodes[$responseCode])) { 
  81. throw new TException(sprintf('tpay.com server return %s', static::$httpCodes[$responseCode])); 
  82. } else { 
  83. throw new TException('Unexpected response from tpay server');