Request.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Request.php UTF-8
  4. * 各个请求函数
  5. *
  6. * @date : 2016年11月16日下午2:45:36
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 7.0
  11. * @modified: 2016年11月16日下午2:45:36
  12. */
  13. /**
  14. * Rsaauth.class.php UTF-8
  15. * 对称 非对称加密 请求与返回
  16. *
  17. * @date : 2016年11月9日下午11:46:45
  18. *
  19. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  20. * @author : wuyonghong <wyh@huosdk.com>
  21. * @version : HUOSDK 7.0
  22. * @modified: 2016年11月9日下午11:46:45
  23. */
  24. namespace huo\controller\request;
  25. use huo\controller\common\Base;
  26. use think\Log;
  27. class Request extends Base {
  28. /**
  29. * CP 回调请求函数
  30. *
  31. * @param $url string 请求地址与端口
  32. * @param $params string post数据
  33. *
  34. * @return int 请求结果
  35. */
  36. public static function cpPayback($url, $params) {
  37. $curl = curl_init(); //初始化curl
  38. curl_setopt($curl, CURLOPT_URL, $url);
  39. curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
  41. curl_setopt($curl, CURLOPT_POST, 1); // post传输数据
  42. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);// post传输数据
  43. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  44. //https 请求
  45. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  46. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  48. }
  49. $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
  50. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  51. //https 请求
  52. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  53. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  54. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  55. }
  56. $responseText = curl_exec($curl);
  57. curl_close($curl);
  58. $rs = strtoupper($responseText);
  59. if ('SUCCESS' == $rs) {
  60. $result = 1;
  61. } else {
  62. $result = 0;
  63. }
  64. return $result;
  65. }
  66. /**
  67. * CP 回调请求函数
  68. *
  69. * @param string $url 请求地址与端口
  70. * @param array $params post数据
  71. *
  72. * @return mixed|false 请求失败为false 其他为请求结果
  73. */
  74. public static function httpJsonpost($url, $params) {
  75. $ch = curl_init();
  76. curl_setopt($ch, CURLOPT_POST, 1);
  77. curl_setopt($ch, CURLOPT_URL, $url);
  78. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  79. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  81. //https 请求
  82. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  83. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  84. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  85. }
  86. curl_setopt(
  87. $ch, CURLOPT_HTTPHEADER, array(
  88. 'Content-Type: application/json; charset=utf-8',
  89. 'Content-Length: '.strlen($params))
  90. );
  91. $_rc = curl_exec($ch);
  92. $_err_no = curl_errno($ch);
  93. if (0 != $_err_no) {
  94. Log::write(array($url, $params, $_err_no, $_rc), Log::ERROR);
  95. return false;
  96. } else {
  97. return $_rc;
  98. }
  99. }
  100. /**
  101. * @param string $url
  102. * @param bool $params
  103. * @param int $is_post
  104. *
  105. * @param array $header
  106. *
  107. * @return bool|mixed
  108. */
  109. public static function http($url, $params = false, $is_post = 0, $header = []) {
  110. $ch = curl_init();
  111. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  112. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  113. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  115. if (empty($header)) {
  116. $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
  117. }
  118. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  119. //https 请求
  120. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  122. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  123. }
  124. if ($is_post) {
  125. curl_setopt($ch, CURLOPT_POST, true);
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  127. curl_setopt($ch, CURLOPT_URL, $url);
  128. } else {
  129. if ($params) {
  130. curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
  131. } else {
  132. curl_setopt($ch, CURLOPT_URL, $url);
  133. }
  134. }
  135. $response = curl_exec($ch);
  136. if ($response === false) {
  137. return false;
  138. }
  139. curl_close($ch);
  140. return $response;
  141. }
  142. }