GTHttpManager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. require_once(dirname(__FILE__) . '/' . '../exception/GTException.php');
  3. class GTHttpManager
  4. {
  5. const HTTP_METHOD_POST = "post";
  6. const HTTP_METHOD_PUT = "put";
  7. const HTTP_METHOD_GET = "get";
  8. const HTTP_METHOD_DELETE = "del";
  9. static $curls = array();
  10. private static function request($url, $data, $gzip, $method,$headers)
  11. {
  12. if(!isset(GTHttpManager::$curls[$url])){
  13. $curl = curl_init($url);
  14. GTHttpManager::$curls[$url] = $curl;
  15. }
  16. $curl = GTHttpManager::$curls[$url];
  17. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  18. curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
  19. curl_setopt($curl, CURLOPT_USERAGENT, 'GeTui RAS2 PHP/1.0');
  20. curl_setopt($curl, CURLOPT_FORBID_REUSE, 0);
  21. curl_setopt($curl, CURLOPT_FRESH_CONNECT, 0);
  22. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, GTConfig::getHttpConnectionTimeOut());
  23. curl_setopt($curl, CURLOPT_TIMEOUT_MS, GTConfig::getHttpSoTimeOut());
  24. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  25. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  26. $header = null;
  27. if ($headers != null){
  28. array_push($headers,"Content-Type:application/json;charset=UTF-8", "Connection: Keep-Alive");
  29. $header = $headers;
  30. }else{
  31. $header = array("Content-Type:application/json;charset=UTF-8", "Connection: Keep-Alive");
  32. }
  33. if ($gzip) {
  34. $data = gzencode($data, 9);
  35. array_push($header,'Accept-Encoding:gzip');
  36. array_push($header,'Content-Encoding:gzip');
  37. curl_setopt($curl, CURLOPT_ENCODING, "gzip");
  38. }
  39. curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
  40. switch ($method){
  41. case self::HTTP_METHOD_GET:
  42. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
  43. break;
  44. case self::HTTP_METHOD_POST:
  45. //post
  46. curl_setopt($curl, CURLOPT_POST, 1);
  47. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  48. break;
  49. case self::HTTP_METHOD_DELETE:
  50. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  51. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  52. break;
  53. case self::HTTP_METHOD_PUT:
  54. curl_setopt($curl, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
  55. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  56. break;
  57. }
  58. $curl_version = curl_version();
  59. if ($curl_version['version_number'] >= 462850) {
  60. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 30000);
  61. curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
  62. }
  63. // 通过代理访问接口需要在此处配置代理
  64. curl_setopt ($curl, CURLOPT_PROXY, GTConfig::getHttpProxyIp());
  65. curl_setopt($curl,CURLOPT_PROXYPORT,GTConfig::getHttpProxyPort());
  66. curl_setopt($curl, CURLOPT_PROXYUSERNAME, GTConfig::getHttpProxyUserName());
  67. curl_setopt($curl, CURLOPT_PROXYPASSWORD, GTConfig::getHttpProxyPasswd());
  68. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // return don't print
  69. curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超时时间
  70. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  71. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
  72. curl_setopt($curl, CURLOPT_MAXREDIRS, 7); //HTTp定向级别
  73. //请求失败有3次重试机会
  74. $result = GTHttpManager::exeBySetTimes(GTConfig::getHttpTryCount(), $curl);
  75. return $result;
  76. }
  77. public static function httpRequest($url, $params,$headers, $gzip = false, $method)
  78. {
  79. $data = json_encode($params);
  80. $result = null;
  81. try {
  82. $resp = GTHttpManager::request($url, $data, $gzip,$method,$headers);
  83. $result = json_decode($resp, true);
  84. return $result;
  85. } catch (Exception $e) {
  86. throw new GTException($params["request_id"],"httpPost:[".$url."] [" .$data." ] [ ".$result."]:",$e);
  87. }
  88. }
  89. private static function exeBySetTimes($count, $curl)
  90. {
  91. $result = curl_exec($curl);
  92. $info = curl_getinfo($curl);
  93. $code = $info["http_code"];
  94. if (curl_errno($curl) != 0 && $code != 200) {
  95. $count--;
  96. if ($count > 0) {
  97. $result = GTHttpManager::exeBySetTimes($count, $curl);
  98. } else {
  99. if ($code == 0 || $code == 404 || $code == 504){
  100. throw new GTException("connect failed, code = ".strval($code));
  101. }
  102. }
  103. }
  104. return $result;
  105. }
  106. }