Http.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Qcloud_cos;
  3. function my_curl_reset($handler)
  4. {
  5. curl_setopt($handler, CURLOPT_URL, '');
  6. curl_setopt($handler, CURLOPT_HTTPHEADER, array());
  7. curl_setopt($handler, CURLOPT_POSTFIELDS, array());
  8. curl_setopt($handler, CURLOPT_TIMEOUT, 0);
  9. curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
  10. curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
  11. }
  12. class Http
  13. {
  14. public static $_httpInfo = '';
  15. public static $_curlHandler;
  16. /**
  17. * send http request
  18. * @param array $rq http请求信息
  19. * url : 请求的url地址
  20. * method : 请求方法,'get', 'post', 'put', 'delete', 'head'
  21. * data : 请求数据,如有设置,则method为post
  22. * header : 需要设置的http头部
  23. * host : 请求头部host
  24. * timeout : 请求超时时间
  25. * cert : ca文件路径
  26. * ssl_version: SSL版本号
  27. * @return string http请求响应
  28. */
  29. public static function send($rq) {
  30. if (self::$_curlHandler) {
  31. if (function_exists('curl_reset')) {
  32. curl_reset(self::$_curlHandler);
  33. } else {
  34. my_curl_reset(self::$_curlHandler);
  35. }
  36. } else {
  37. self::$_curlHandler = curl_init();
  38. }
  39. curl_setopt(self::$_curlHandler, CURLOPT_URL, $rq['url']);
  40. switch (true) {
  41. case isset($rq['method']) && in_array(strtolower($rq['method']), array('get', 'post', 'put', 'delete', 'head')):
  42. $method = strtoupper($rq['method']);
  43. break;
  44. case isset($rq['data']):
  45. $method = 'POST';
  46. break;
  47. default:
  48. $method = 'GET';
  49. }
  50. $header = isset($rq['header']) ? $rq['header'] : array();
  51. $header[] = 'Method:'.$method;
  52. $header[] = 'User-Agent:'.Conf::getUA();
  53. $header[] = 'Connection: keep-alive';
  54. if ('POST' == $method) {
  55. $header[] = 'Expect: ';
  56. }
  57. isset($rq['host']) && $header[] = 'Host:'.$rq['host'];
  58. curl_setopt(self::$_curlHandler, CURLOPT_HTTPHEADER, $header);
  59. curl_setopt(self::$_curlHandler, CURLOPT_RETURNTRANSFER, 1);
  60. curl_setopt(self::$_curlHandler, CURLOPT_CUSTOMREQUEST, $method);
  61. isset($rq['timeout']) && curl_setopt(self::$_curlHandler, CURLOPT_TIMEOUT, $rq['timeout']);
  62. isset($rq['data']) && in_array($method, array('POST', 'PUT')) && curl_setopt(self::$_curlHandler, CURLOPT_POSTFIELDS, $rq['data']);
  63. $ssl = substr($rq['url'], 0, 8) == "https://" ? true : false;
  64. if( isset($rq['cert'])){
  65. curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER,true);
  66. curl_setopt(self::$_curlHandler, CURLOPT_CAINFO, $rq['cert']);
  67. curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST,2);
  68. if (isset($rq['ssl_version'])) {
  69. curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, $rq['ssl_version']);
  70. } else {
  71. curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, 4);
  72. }
  73. }else if( $ssl ){
  74. curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER,false); //true any ca
  75. curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST,1); //check only host
  76. if (isset($rq['ssl_version'])) {
  77. curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, $rq['ssl_version']);
  78. } else {
  79. curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, 4);
  80. }
  81. }
  82. $ret = curl_exec(self::$_curlHandler);
  83. self::$_httpInfo = curl_getinfo(self::$_curlHandler);
  84. return $ret;
  85. }
  86. public static function info() {
  87. return self::$_httpInfo;
  88. }
  89. }