HttpsClient.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace ACES\Common;
  3. use RuntimeException;
  4. use ACES\Common\Exception as Ex;
  5. use Monolog\Logger;
  6. use Monolog\Handler\StreamHandler;
  7. use Monolog\Formatter\LineFormatter;
  8. if(!defined("LOGCONSOLE")){
  9. define("LOGCONSOLE", __DIR__."/../../../tde.log");
  10. }
  11. if(!defined("LOGLEVEL")){
  12. define("LOGLEVEL", Logger::ERROR);
  13. }
  14. /**
  15. * ACES PHP HttpsClient
  16. * <P>
  17. *
  18. * @author JD Data Security Team (tenma.lin, wei.gao, mozhiyan, xuyina)
  19. * @version 1.0
  20. *
  21. */
  22. final class HttpsClient {
  23. private static $CAPath = '';
  24. public static function loadCACert($isProd){
  25. $prodCaPath = __DIR__ . Constants::PROD_CA_RELATIVE_PATH;
  26. $betaCaPath = __DIR__ . Constants::BETA_CA_RELATIVE_PATH;
  27. self::$CAPath = $isProd ? $prodCaPath : $betaCaPath;
  28. }
  29. public static function postForm($requestURL, $params) {
  30. // confige log
  31. $log = new Logger('httpsClient');
  32. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message%\r\n");
  33. $handle = new StreamHandler(LOGCONSOLE, LOGLEVEL);
  34. $handle->setFormatter($formatter);
  35. $log->pushHandler($handle);
  36. $response = '';
  37. $rootCause = '';
  38. $hasConn = False;
  39. for($retry=0;$retry<Constants::HTTP_RETRY_MAX && !$hasConn;$retry++){
  40. try {
  41. $ch = curl_init($requestURL);
  42. curl_setopt($ch, CURLOPT_POST, True);
  43. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, Constants::HTTP_TIMEOUT);
  44. curl_setopt($ch, CURLOPT_TIMEOUT_MS, Constants::HTTP_TIMEOUT);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, True); // True to return the transfer as a string of the return value
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // True verify the peer's certificate
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // True verify the peer's certificate
  48. curl_setopt($ch, CURLOPT_HTTPHEADER, self::httpFormHeader());
  49. $payload = http_build_query($params);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  51. $response = curl_exec($ch);
  52. // Get the error number for the last cURL operation, if no error occurs then return 0.
  53. if(curl_errno($ch)){
  54. $rootCause = curl_error($ch);
  55. throw new Ex\HttpConnectionException(curl_error($ch));
  56. }
  57. $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  58. if($response_code != 200){
  59. $rootCause = "Wrong http reponse code:".$response_code;
  60. throw new Ex\HttpConnectionException("Wrong http reponse code:".$response_code);
  61. }
  62. // todo: can this way keep connection alive?
  63. // curl_close($ch);
  64. $hasConn = True;
  65. } catch (Ex\HttpConnectionException $e) {
  66. $log->info("Https postJson error: " . $e->getMessage());
  67. }
  68. }
  69. if(!$hasConn){
  70. $log->error("HTTPS Client cannot establish connection:".$rootCause);
  71. throw new RuntimeException("HTTPS Client cannot establish connection:".$rootCause);
  72. }
  73. return $response;
  74. }
  75. public static function postJson($requestURL, $payload) {
  76. // confige log
  77. $log = new Logger('httpsClient');
  78. $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message%\r\n");
  79. $handle = new StreamHandler(LOGCONSOLE, LOGLEVEL);
  80. $handle->setFormatter($formatter);
  81. $log->pushHandler($handle);
  82. $response = '';
  83. $rootCause = '';
  84. $hasConn = False;
  85. for($retry=0;$retry<Constants::HTTP_RETRY_MAX && !$hasConn;$retry++){
  86. try {
  87. $ch = curl_init($requestURL);
  88. curl_setopt($ch, CURLOPT_POST, True);
  89. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, Constants::HTTP_TIMEOUT);
  90. curl_setopt($ch, CURLOPT_TIMEOUT_MS, Constants::HTTP_TIMEOUT);
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER, True); // True to return the transfer as a string of the return value
  92. curl_setopt($ch, CURLOPT_CAINFO, self::$CAPath); // CA certificate
  93. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, True); // True verify the peer's certificate
  94. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // True verify the peer's certificate
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // True verify the peer's certificate
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, self::httpJsonHeader());
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  98. $response = curl_exec($ch);
  99. // Get the error number for the last cURL operation, if no error occurs then return 0.
  100. if(curl_errno($ch)){
  101. $rootCause = curl_error($ch);
  102. throw new Ex\HttpConnectionException(curl_error($ch));
  103. }
  104. $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  105. if($response_code != 200){
  106. $rootCause = "Wrong http reponse code:".$response_code;
  107. throw new Ex\HttpConnectionException("Wrong http reponse code:".$response_code);
  108. }
  109. // todo: can this way keep connection alive?
  110. // curl_close($ch);
  111. $hasConn = True;
  112. } catch (Ex\HttpConnectionException $e) {
  113. $log->info("Https postJson error: " . $e->getMessage());
  114. }
  115. }
  116. if(!$hasConn){
  117. $log->error("HTTPS Client cannot establish connection:".$rootCause);
  118. throw new RuntimeException("HTTPS Client cannot establish connection:".$rootCause);
  119. }
  120. return $response;
  121. }
  122. public static function httpJsonHeader() {
  123. $header = array(
  124. "Accept:application/json",
  125. "Content-Type:application/json;charset=UTF-8",
  126. "Connection:keep-alive"
  127. );
  128. return $header;
  129. }
  130. public static function httpFormHeader() {
  131. $header = array(
  132. "Accept:application/json",
  133. "Content-Type:application/x-www-form-urlencoded;charset=UTF-8",
  134. "Connection:keep-alive"
  135. );
  136. return $header;
  137. }
  138. }