http.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 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. $val = implode(',',$headers);
  80. Log::record("post oms header={$val}",Log::DEBUG);
  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. }