http.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(),&$net_errno = 0)
  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_RETURNTRANSFER, true);
  18. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  19. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  20. curl_setopt($ci, CURLOPT_HEADER, false);
  21. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60);
  22. curl_setopt($ci, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  23. curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);
  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 ($params 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. $net_errno = curl_errno($ci);
  63. Log::record("netmodule http_request errno={$net_errno} err={$err} url={$url}",Log::ERR);
  64. }
  65. else
  66. {
  67. $code = curl_getinfo($ci,CURLINFO_HTTP_CODE);
  68. if($code != 200) {
  69. Log::record("netmodule http_request http errno={$code} url={$url}",Log::ERR);
  70. }
  71. }
  72. curl_close($ci);
  73. return $response;
  74. }
  75. function http_post_data($url, $body, $headers = array(),&$net_errno = 0)
  76. {
  77. if (!function_exists('curl_init')) {
  78. return false;
  79. }
  80. $ci = curl_init();
  81. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  82. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  84. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  85. curl_setopt($ci, CURLOPT_HEADER, false);
  86. curl_setopt($ci, CURLOPT_POSTFIELDS, $body);
  87. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  88. curl_setopt($ci, CURLOPT_URL, $url);
  89. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60);
  90. curl_setopt($ci, CURLOPT_POST, TRUE);
  91. curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);
  92. if (!empty($headers)) {
  93. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  94. }
  95. $response = curl_exec($ci);
  96. if($response === false) {
  97. $err = curl_error($ci);
  98. $net_errno = curl_errno($ci);
  99. Log::record("netmodule http_post_data errno={$net_errno} err={$err} url={$url}",Log::ERR);
  100. }
  101. else
  102. {
  103. $code = curl_getinfo($ci,CURLINFO_HTTP_CODE);
  104. if($code != 200) {
  105. Log::record("netmodule http_request http errno={$code} url={$url}",Log::ERR);
  106. }
  107. }
  108. curl_close($ci);
  109. return $response;
  110. }
  111. function https_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array(),&$net_errno = 0)
  112. {
  113. if (!function_exists('curl_init')) {
  114. return false;
  115. }
  116. $method = strtoupper($method);
  117. $ci = curl_init();
  118. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  119. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
  122. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60);
  123. curl_setopt($ci, CURLOPT_HEADER, false);
  124. curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);
  125. $headers = (array) $extheaders;
  126. switch ($method)
  127. {
  128. case 'POST':
  129. curl_setopt($ci, CURLOPT_POST, TRUE);
  130. if (!empty($params))
  131. {
  132. if ($multi)
  133. {
  134. foreach ($params as $key => $file) {
  135. $params[$key] = '@' . $file;
  136. }
  137. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  138. $headers[] = 'Expect: ';
  139. }
  140. else {
  141. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  142. }
  143. }
  144. break;
  145. case 'DELETE':
  146. case 'GET':
  147. if($method == 'DELETE') {
  148. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  149. }
  150. if (!empty($params)) {
  151. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  152. }
  153. break;
  154. }
  155. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  156. curl_setopt($ci, CURLOPT_URL, $url);
  157. if ($headers) {
  158. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  159. }
  160. $response = curl_exec($ci);
  161. if($response === false) {
  162. $err = curl_error($ci);
  163. $net_errno = curl_errno($ci);
  164. Log::record("netmodule https_request errno={$net_errno} err={$err} url={$url}",Log::ERR);
  165. }
  166. else
  167. {
  168. $code = curl_getinfo($ci,CURLINFO_HTTP_CODE);
  169. if($code != 200) {
  170. Log::record("netmodule http_request http errno={$code} url={$url}",Log::ERR);
  171. }
  172. }
  173. curl_close($ci);
  174. return $response;
  175. }