http.php 5.0 KB

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