暂无描述

helper.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * [reValue 格式化返回数据]
  4. * @param [type] $data [返回数据]
  5. * @param integer $code [返回状态码]
  6. * @param [array] $message [消息数组]
  7. * @return [string] [json字符串]
  8. */
  9. function reValue($code = 0, $data = null, $message = null)
  10. {
  11. $message = !empty($message) ? $message : \App\Support\Message::getMessage($code);
  12. // if (!is_array($message)) {
  13. // $message = [$message];
  14. // }
  15. $value = json_encode(["res" => $data, "message" => $message, "code" => $code]);
  16. return $value;
  17. }
  18. /**
  19. * [randomStr 获取随机字符串]
  20. * @Author mzb
  21. * @DateTime 2018-04-02T16:50:58+0800
  22. * @param integer $length [获取字符串长度]
  23. * @param boolean $isNum [是否获取数字类型字符串]
  24. * @return [type] [description]
  25. */
  26. function randomStr($length = 4, $isNum = true)
  27. {
  28. $str = '0123456789';
  29. if (!$isNum) {
  30. $str .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  31. }
  32. $strLength = strlen($str);
  33. $length = strlen($str) >= $length ? $length : $strLength;
  34. $str = str_shuffle($str);
  35. return substr($str, 0, $length);
  36. }
  37. /**
  38. * [geturl curl发送get请求]
  39. * @Author mzb
  40. * @DateTime 2018-06-11T11:24:59+0800
  41. * @param [type] $url [description]
  42. * @return [type] [description]
  43. */
  44. // function geturl($url)
  45. // {
  46. // $headerArray = array("Content-type:application/json;", "Accept:application/json");
  47. // $ch = curl_init();
  48. // curl_setopt($ch, CURLOPT_URL, $url);
  49. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  50. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  51. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  52. // curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  53. // $output = curl_exec($ch);
  54. // curl_close($ch);
  55. // $output = json_decode($output, true);
  56. // return $output;
  57. // }
  58. function curlInit($url, $fields = null, $method, $headers = null, $timeout = 10)
  59. {
  60. $ch = curl_init();
  61. // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8','Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  62. // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
  63. // 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  64. if (isset($headers)) {
  65. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  66. }
  67. // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  69. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  71. // curl_setopt($ch, CURLOPT_HEADER, true);
  72. // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  73. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  74. if ($method == "Post") {
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. curl_setopt($ch, CURLOPT_POST, true);
  77. if (isset($fields)) {
  78. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  79. }
  80. } else if($method == 'json') {
  81. curl_setopt($ch, CURLOPT_URL, $url);
  82. curl_setopt($ch, CURLOPT_POST, true);
  83. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  84. } else {
  85. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($fields));
  86. curl_setopt($ch, CURLOPT_POST, false);
  87. }
  88. $content = curl_exec($ch);
  89. // var_dump($content);
  90. // $result = "";
  91. // if ($content !== false) {
  92. // } else {
  93. // $result = json_encode(array(
  94. // "reason" => "network error or timeout",
  95. // ));
  96. // }
  97. // Close connection
  98. curl_close($ch);
  99. return $content;
  100. }
  101. /*
  102. * 以下代码实现PHP sha256() sha256_file() sha512() sha512_file() PHP 5.1.2+完美兼容
  103. * @param string $data 要计算散列值的字符串
  104. * @param boolean $rawOutput 为true时返回原始二进制数据,否则返回字符串
  105. * @param string file 要计算散列值的文件名,可以是单独的文件名,也可以包含路径,绝对路径相对路径都可以
  106. * @return boolean | string 参数无效或者文件不存在或者文件不可读时返回false,计算成功则返回对应的散列值
  107. * @notes 使用示例 sha256('mrdede.com') sha512('mrdede.com') sha256_file('index.php') sha512_file('index.php')
  108. */
  109. /* PHP sha256() */
  110. // function sha256($data, $rawOutput = false)
  111. // {
  112. // if (!is_scalar($data)) {
  113. // return false;
  114. // }
  115. // $data = (string) $data;
  116. // $rawOutput = !!$rawOutput;
  117. // return hash('sha256', $data, $rawOutput);
  118. // }
  119. function getMillisecond()
  120. {
  121. list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
  122. return (float) sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
  123. }
  124. function curlPost($url, $data, $timeout = 3, $headerAry = '')
  125. {
  126. $ch = curl_init();
  127. curl_setopt($ch, CURLOPT_URL, $url);
  128. curl_setopt($ch, CURLOPT_POST, true);
  129. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  130. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  131. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  132. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  133. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  134. curl_setopt($ch, CURLOPT_HEADER, false);
  135. if ($headerAry != '') {
  136. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerAry);
  137. }
  138. $res = curl_exec($ch);
  139. return $res;
  140. }