http.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * http请求函数
  4. *
  5. *
  6. *
  7. */
  8. function http_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
  9. {
  10. if (!function_exists('curl_init')) {
  11. return false;
  12. }
  13. $method = strtoupper($method);
  14. $ci = curl_init();
  15. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  16. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  17. curl_setopt($ci, CURLOPT_TIMEOUT, 3);
  18. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  20. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  21. curl_setopt($ci, CURLOPT_HEADER, false);
  22. $headers = (array) $extheaders;
  23. switch ($method)
  24. {
  25. case 'POST':
  26. curl_setopt($ci, CURLOPT_POST, TRUE);
  27. if (!empty($params))
  28. {
  29. if ($multi)
  30. {
  31. foreach ($multi as $key => $file) {
  32. $params[$key] = '@' . $file;
  33. }
  34. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  35. $headers[] = 'Expect: ';
  36. }
  37. else {
  38. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  39. }
  40. }
  41. break;
  42. case 'DELETE':
  43. case 'GET':
  44. if($method == 'DELETE') {
  45. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  46. }
  47. if (!empty($params)) {
  48. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  49. }
  50. break;
  51. }
  52. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  53. curl_setopt($ci, CURLOPT_URL, $url);
  54. if ($headers) {
  55. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  56. }
  57. $response = curl_exec($ci);
  58. curl_close($ci);
  59. return $response;
  60. }
  61. function http_post_data($url, $body, $headers = array())
  62. {
  63. if (!function_exists('curl_init')) {
  64. return false;
  65. }
  66. $ci = curl_init();
  67. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  68. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  69. curl_setopt($ci, CURLOPT_TIMEOUT, 15);
  70. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  72. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  73. curl_setopt($ci, CURLOPT_HEADER, false);
  74. curl_setopt($ci, CURLOPT_POSTFIELDS, $body);
  75. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  76. curl_setopt($ci, CURLOPT_URL, $url);
  77. if (!empty($headers)) {
  78. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  79. }
  80. $response = curl_exec($ci);
  81. if($response == false) {
  82. $err = curl_error($ci);
  83. Log::record("http_post_data err={$err}",Log::ERR);
  84. }
  85. curl_close($ci);
  86. return $response;
  87. }
  88. function https_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
  89. {
  90. if (!function_exists('curl_init')) {
  91. return false;
  92. }
  93. $method = strtoupper($method);
  94. $ci = curl_init();
  95. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  96. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  97. curl_setopt($ci, CURLOPT_TIMEOUT, 3);
  98. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  99. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  100. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
  101. curl_setopt($ci, CURLOPT_HEADER, false);
  102. $headers = (array) $extheaders;
  103. switch ($method)
  104. {
  105. case 'POST':
  106. curl_setopt($ci, CURLOPT_POST, TRUE);
  107. if (!empty($params))
  108. {
  109. if ($multi)
  110. {
  111. foreach ($multi as $key => $file) {
  112. $params[$key] = '@' . $file;
  113. }
  114. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  115. $headers[] = 'Expect: ';
  116. }
  117. else {
  118. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  119. }
  120. }
  121. break;
  122. case 'DELETE':
  123. case 'GET':
  124. if($method == 'DELETE') {
  125. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  126. }
  127. if (!empty($params)) {
  128. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  129. }
  130. break;
  131. }
  132. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  133. curl_setopt($ci, CURLOPT_URL, $url);
  134. if ($headers) {
  135. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  136. }
  137. $response = curl_exec($ci);
  138. curl_close($ci);
  139. return $response;
  140. }