http.php 4.8 KB

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