Curl.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. class Curl{
  3. //packaging of the original curl api
  4. public static function post($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e", $timeout = 5){
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  7. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10. if($basicAuth)
  11. $header = Arr::merge($header, array("Authorization: Basic ".base64_encode($basicAuth)));
  12. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  13. if($array){
  14. $array = http_build_query($array);
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
  16. }
  17. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. return $data;
  21. }
  22. //packaging of the original curl api
  23. public static function http_post_json($url, $jsonStr)
  24. {
  25. $ch = curl_init();
  26. curl_setopt($ch, CURLOPT_POST, 1);
  27. curl_setopt($ch, CURLOPT_URL, $url);
  28. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  31. 'Content-Type: application/json; charset=utf-8',
  32. 'Content-Length: ' . strlen($jsonStr)
  33. )
  34. );
  35. $response = curl_exec($ch);
  36. curl_close($ch);
  37. return $response;
  38. }
  39. public static function http_post_Basic_json($url, $jsonStr,$basicAuth = "zxhx:183971ee2455430abbc0328f15050913")
  40. {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_POST, 1);
  43. curl_setopt($ch, CURLOPT_URL, $url);
  44. //设置超时
  45. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  46. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  50. 'Content-Type: application/json; charset=utf-8',
  51. 'Content-Length: ' . strlen($jsonStr),
  52. 'Authorization: Basic '.base64_encode($basicAuth)
  53. )
  54. );
  55. $response = curl_exec($ch);
  56. curl_close($ch);
  57. return $response;
  58. }
  59. public static function http_post_gzip($url, $flag = 1, $data)
  60. {
  61. $result = FALSE;
  62. $ch = @curl_init();
  63. if ($ch) {
  64. // 不输出头部
  65. curl_setopt($ch, CURLOPT_HEADER, 0);
  66. //设置超时
  67. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  68. curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  69. // curl_exec 获取到的内容不直接输出, 而是返回
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  71. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  73. 'Content-Type: application/json',
  74. 'Content-Length: ' . strlen($data),
  75. ));
  76. // 请求重启路由器的地址 传参 进行重启
  77. curl_setopt($ch, CURLOPT_URL, $url);
  78. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  79. if ($flag == 2) {
  80. curl_setopt($ch, CURLOPT_POST, 1);
  81. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  82. }
  83. $result = curl_exec($ch);
  84. if (curl_errno($ch)) {
  85. throw new Exception('curl_errno:' . curl_errno($ch));
  86. exit();
  87. }
  88. // 释放资源
  89. curl_close($ch);
  90. }
  91. return $result;
  92. }
  93. public static function http_post_gzip_json($url, $jsonStr)
  94. {
  95. $ch = curl_init();
  96. curl_setopt($ch, CURLOPT_POST, 1);
  97. curl_setopt($ch, CURLOPT_URL, $url);
  98. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  99. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  100. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  101. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  102. 'Content-Type: application/json; charset=utf-8',
  103. 'Content-Length: ' . strlen($jsonStr)
  104. )
  105. );
  106. $response = curl_exec($ch);
  107. curl_close($ch);
  108. return $response;
  109. }
  110. public static function http_get($url)
  111. {
  112. $ch = curl_init();
  113. curl_setopt ($ch, CURLOPT_URL, $url);
  114. //不下载
  115. curl_setopt($ch, CURLOPT_NOBODY, 1);
  116. //设置超时
  117. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
  118. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  119. //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  120. curl_exec($ch);
  121. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  122. if($http_code == 200) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. }