http.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 NULL;
  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. {
  40. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  41. }
  42. }
  43. break;
  44. case 'DELETE':
  45. case 'GET':
  46. $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  47. if (!empty($params)) {
  48. $url = $url . (strpos($url, '?') ? '&' : '?')
  49. . (is_array($params) ? http_build_query($params) : $params);
  50. }
  51. echo $url;
  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. curl_close($ci);
  61. return $response;
  62. }
  63. function http_post_data($url, $body, $headers = array())
  64. {
  65. if (!function_exists('curl_init')) {
  66. return NULL;
  67. }
  68. $ci = curl_init();
  69. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  70. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
  71. curl_setopt($ci, CURLOPT_TIMEOUT, 3);
  72. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  73. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  74. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  75. curl_setopt($ci, CURLOPT_HEADER, false);
  76. curl_setopt($ci, CURLOPT_POSTFIELDS, $body);
  77. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  78. curl_setopt($ci, CURLOPT_URL, $url);
  79. if ($headers) {
  80. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  81. }
  82. $response = curl_exec($ci);
  83. curl_close($ci);
  84. return $response;
  85. }