人人商城

http_client.php 4.4KB

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