Utils.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yqj
  5. * Date: 2017/5/2
  6. * Time: 00:19
  7. */
  8. namespace app\Lib;
  9. class Utils
  10. {
  11. /**
  12. * 发送请求
  13. */
  14. public static function curlRequest($url, $data)
  15. {
  16. $headers = ["Content-type: application/json;charset='utf-8'"];
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_POST, 1);
  20. curl_setopt($ch, CURLOPT_HEADER, 0);
  21. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  23. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  24. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  25. $respon = curl_exec($ch);
  26. return $respon;
  27. }
  28. /**
  29. * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  30. */
  31. public static function createLinkstring($para)
  32. {
  33. $arg = "";
  34. while (list ($key, $val) = each($para)) {
  35. $arg .= $key . "=" . $val . "&";
  36. }
  37. //去掉最后一个&字符
  38. $arg = substr($arg, 0, count($arg) - 2);
  39. //如果存在转义字符,那么去掉转义
  40. if (get_magic_quotes_gpc()) {
  41. $arg = stripslashes($arg);
  42. }
  43. // dd($arg);
  44. return str_replace(" ", "", $arg);
  45. //return $arg.replace(" ","");
  46. }
  47. /**
  48. * 下载远程图片到本地
  49. *
  50. * @param string $url 远程文件地址
  51. * @param string $filename 保存后的文件名(为空时则为随机生成的文件名,否则为原文件名)
  52. * @param array $fileType 允许的文件类型
  53. * @param string $dirName 文件保存的路径(路径其余部分根据时间系统自动生成)
  54. * @param int $type 远程获取文件的方式
  55. * @return array 返回文件名、文件的保存路径
  56. */
  57. public static function downloadImage($url, $fileName = '', $dirName, $fileType = array('jpg', 'gif', 'png'), $type = 1)
  58. {
  59. if ($url == '') {
  60. return false;
  61. }
  62. // 获取文件原文件名
  63. $defaultFileName = basename($url);
  64. // 获取文件类型
  65. $suffix = substr(strrchr($url, '.'), 1);
  66. if (!in_array($suffix, $fileType)) {
  67. return false;
  68. }
  69. // 设置保存后的文件名
  70. $fileName = $fileName == '' ? time() . rand(0, 9) . '.' . $suffix : $defaultFileName;
  71. // 获取远程文件资源
  72. if ($type) {
  73. $ch = curl_init();
  74. $timeout = 30;
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  77. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  78. $file = curl_exec($ch);
  79. curl_close($ch);
  80. } else {
  81. ob_start();
  82. readfile($url);
  83. $file = ob_get_contents();
  84. ob_end_clean();
  85. }
  86. // 设置文件保存路径
  87. //$dirName = $dirName . '/' . date('Y', time()) . '/' . date('m', time()) . '/' . date('d', time());
  88. $dirName = $dirName . '/' . date('Ym', time());
  89. if (!file_exists($dirName)) {
  90. mkdir($dirName, 0777, true);
  91. }
  92. if(!file_exists($dirName . '/' . $fileName)){
  93. // 保存文件
  94. $res = fopen($dirName . '/' . $fileName, 'a');
  95. fwrite($res, $file);
  96. fclose($res);
  97. }
  98. return array(
  99. 'fileName' => $fileName,
  100. 'saveDir' => $dirName
  101. );
  102. }
  103. public static function startWith($str, $needle)
  104. {
  105. return strpos($str, $needle) === 0;
  106. }
  107. public static function endWith($haystack, $needle)
  108. {
  109. $length = strlen($needle);
  110. if ($length == 0) return true;
  111. return (substr($haystack, -$length) === $needle);
  112. }
  113. }