HttpCurl.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace AmazonPay;
  3. /* Class HttpCurl
  4. * Handles Curl POST function for all requests
  5. */
  6. require_once 'HttpCurlInterface.php';
  7. class HttpCurl implements HttpCurlInterface
  8. {
  9. private $config = array();
  10. private $header = false;
  11. private $accessToken = null;
  12. private $curlResponseInfo = null;
  13. private $headerArray = array();
  14. /* Takes user configuration array as input
  15. * Takes configuration for API call or IPN config
  16. */
  17. public function __construct($config = null)
  18. {
  19. $this->config = $config;
  20. }
  21. /* Setter for boolean header to get the user info */
  22. public function setHttpHeader()
  23. {
  24. $this->header = true;
  25. }
  26. /* Setter for Access token to get the user info */
  27. public function setAccessToken($accesstoken)
  28. {
  29. $this->accessToken = $accesstoken;
  30. }
  31. /* Add the common Curl Parameters to the curl handler $ch
  32. * Also checks for optional parameters if provided in the config
  33. * config['cabundle_file']
  34. * config['proxy_port']
  35. * config['proxy_host']
  36. * config['proxy_username']
  37. * config['proxy_password']
  38. */
  39. protected function commonCurlParams($url,$userAgent)
  40. {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_URL, $url);
  43. curl_setopt($ch, CURLOPT_PORT, 443);
  44. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  45. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47. if (!is_null($this->config['cabundle_file'])) {
  48. curl_setopt($ch, CURLOPT_CAINFO, $this->config['cabundle_file']);
  49. }
  50. if (!empty($userAgent))
  51. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  52. if ($this->config['proxy_host'] != null && $this->config['proxy_port'] != -1) {
  53. curl_setopt($ch, CURLOPT_PROXY, $this->config['proxy_host'] . ':' . $this->config['proxy_port']);
  54. }
  55. if ($this->config['proxy_username'] != null && $this->config['proxy_password'] != null) {
  56. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->config['proxy_username'] . ':' . $this->config['proxy_password']);
  57. }
  58. return $ch;
  59. }
  60. /* POST using curl for the following situations
  61. * 1. API calls
  62. * 2. IPN certificate retrieval
  63. * 3. Get User Info
  64. */
  65. public function httpPost($url, $userAgent = null, $parameters = null)
  66. {
  67. $ch = $this->commonCurlParams($url,$userAgent);
  68. curl_setopt($ch, CURLOPT_POST, true);
  69. curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  70. curl_setopt($ch, CURLOPT_HEADER, false);
  71. $response = $this->execute($ch);
  72. return $response;
  73. }
  74. /* GET using curl for the following situations
  75. * 1. IPN certificate retrieval
  76. * 2. Get User Info
  77. */
  78. public function httpGet($url, $userAgent = null)
  79. {
  80. $ch = $this->commonCurlParams($url,$userAgent);
  81. // Setting the HTTP header with the Access Token only for Getting user info
  82. if ($this->header) {
  83. $this->headerArray[] = 'Authorization: bearer ' . $this->accessToken;
  84. }
  85. $response = $this->execute($ch);
  86. return $response;
  87. }
  88. /* Execute Curl request */
  89. /* Protected because will be used by PSP module */
  90. protected function execute($ch)
  91. {
  92. $response = '';
  93. // Ensure we never send the "Expect: 100-continue" header
  94. $this->headerArray[] = 'Expect:';
  95. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArray);
  96. $response = curl_exec($ch);
  97. if ($response === false) {
  98. $error_msg = "Unable to post request, underlying exception of " . curl_error($ch);
  99. curl_close($ch);
  100. throw new \Exception($error_msg);
  101. } else {
  102. $this->curlResponseInfo = curl_getinfo($ch);
  103. }
  104. curl_close($ch);
  105. return $response;
  106. }
  107. /* Get the output of Curl Getinfo */
  108. public function getCurlResponseInfo()
  109. {
  110. return $this->curlResponseInfo;
  111. }
  112. }