http.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, 30);
  17. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  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. if($response === false) {
  59. $err = curl_error($ci);
  60. }
  61. curl_close($ci);
  62. return $response;
  63. }
  64. function http_post_data($url, $body, $headers = array())
  65. {
  66. if (!function_exists('curl_init')) {
  67. return false;
  68. }
  69. $ci = curl_init();
  70. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  71. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  72. curl_setopt($ci, CURLOPT_TIMEOUT, 15);
  73. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  75. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  76. curl_setopt($ci, CURLOPT_HEADER, false);
  77. curl_setopt($ci, CURLOPT_POSTFIELDS, $body);
  78. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  79. curl_setopt($ci, CURLOPT_URL, $url);
  80. if (!empty($headers)) {
  81. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  82. }
  83. $response = curl_exec($ci);
  84. if($response == false) {
  85. $err = curl_error($ci);
  86. Log::record("http_post_data err={$err}",Log::ERR);
  87. }
  88. curl_close($ci);
  89. return $response;
  90. }
  91. function https_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
  92. {
  93. if (!function_exists('curl_init')) {
  94. return false;
  95. }
  96. $method = strtoupper($method);
  97. $ci = curl_init();
  98. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  99. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  100. curl_setopt($ci, CURLOPT_TIMEOUT, 3);
  101. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  102. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
  104. curl_setopt($ci, CURLOPT_HEADER, false);
  105. $headers = (array) $extheaders;
  106. switch ($method)
  107. {
  108. case 'POST':
  109. curl_setopt($ci, CURLOPT_POST, TRUE);
  110. if (!empty($params))
  111. {
  112. if ($multi)
  113. {
  114. foreach ($multi as $key => $file) {
  115. $params[$key] = '@' . $file;
  116. }
  117. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  118. $headers[] = 'Expect: ';
  119. }
  120. else {
  121. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  122. }
  123. }
  124. break;
  125. case 'DELETE':
  126. case 'GET':
  127. if($method == 'DELETE') {
  128. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  129. }
  130. if (!empty($params)) {
  131. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  132. }
  133. break;
  134. }
  135. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  136. curl_setopt($ci, CURLOPT_URL, $url);
  137. if ($headers) {
  138. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  139. }
  140. $response = curl_exec($ci);
  141. curl_close($ci);
  142. return $response;
  143. }