SmsFree.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Heanup\Library\Sms;
  3. use Heanup\App\Api\Config\Code;
  4. use Heanup\App\Api\Package\Database\Num;
  5. use Heanup\Config\Proxy;
  6. use Heanup\Library\Curl;
  7. use QL\QueryList;
  8. class SmsFree implements Sms
  9. {
  10. /**
  11. * @return array|false|string|string[]
  12. */
  13. public function getCountry()
  14. {
  15. $url = "https://smsfree.cc/api/";
  16. $data = [
  17. 'do' => "Home"
  18. ];
  19. $result = Curl::post($url, $data);
  20. $result = json_decode($result, true);
  21. return $result['Country'];
  22. }
  23. /**
  24. * @param $link
  25. * @return array
  26. */
  27. public function getPhoneNums($link): array
  28. {
  29. $url = "https://smsfree.cc/api/";
  30. $data = [
  31. 'country' => $link,
  32. 'do' => "List"
  33. ];
  34. $result = Curl::post($url, $data, "host.docker.internal:7777");
  35. $result = json_decode($result, true);
  36. $response = $result['Phone'];
  37. $this->insert($link, $result['Phone']);
  38. $total = $result['End'];
  39. for ($i = 2; $i <= $total; $i++) {
  40. $temp = $data;
  41. $temp['page'] = $i;
  42. $result = Curl::post($url, $temp, "host.docker.internal:7777");
  43. $result = json_decode($result, true);
  44. if ($result) {
  45. $this->insert($link, $result['Phone']);
  46. $response = array_merge($response, $result['Phone']);
  47. }
  48. }
  49. return $response;
  50. //
  51. // $ql = (new QueryList)->get($link);
  52. // $end_page_link = $ql->find(".pagination a:contains(End)")->attr("href");
  53. // preg_match("#\d+#", $end_page_link, $all_page);
  54. // $all_page = $all_page[0];
  55. // $data = $ql->find(".h2 span")->texts()->all();
  56. // $this->insert($link, $data);
  57. // if ($all_page > 0) {
  58. // for ($i = 2; $i <= $all_page; $i++) {
  59. // $url = sprintf($link . "%s.html", $i);
  60. // $_ql = (new QueryList)->get($url);
  61. // $_data = $_ql->find(".h2 span")->texts()->all();
  62. // if ($_data) {
  63. // $this->insert($link, $_data);
  64. // $data = array_merge($data, $_data);
  65. // } else {
  66. // echo "总页数:" . $all_page;
  67. // echo "处理异常: " . $url . PHP_EOL;
  68. // }
  69. // }
  70. // }
  71. // return $data;
  72. }
  73. public function getMessage($country, $phone_num): array
  74. {
  75. $url = "https://smsfree.cc/api/";
  76. $_num = str_replace(['+', ' '], ['', ''], $country . $phone_num);
  77. $data = [
  78. 'page' => 1,
  79. 'phone' => $_num,
  80. 'do' => "Message",
  81. ];
  82. $result = Curl::post($url, $data);
  83. $result = json_decode($result, true);
  84. $response = array_map(function ($a) {
  85. $rtime = date("m-d H:i", $a['time']);
  86. $htime = date("H:i", $a['time']);
  87. $time = time() - $a['time'];
  88. if ($time < 60) {
  89. $str = '刚刚';
  90. } elseif ($time < 60 * 60) {
  91. $min = floor($time / 60);
  92. $str = $min . '分钟前';
  93. } elseif ($time < 60 * 60 * 24) {
  94. $h = floor($time / (60 * 60));
  95. $str = $h . '小时前 ';
  96. } elseif ($time < 60 * 60 * 24 * 3) {
  97. $d = floor($time / (60 * 60 * 24));
  98. if ($d == 1) {
  99. $str = '昨天 ' . $htime;
  100. } else {
  101. $str = '前天 ' . $htime;
  102. }
  103. } else {
  104. $str = $rtime;
  105. }
  106. return [
  107. 'from' => $a['from'],
  108. 'time' => $str,
  109. 'message' => $a['data'],
  110. ];
  111. }, $result['Message']);
  112. return $response;
  113. }
  114. private function insert($link, $data)
  115. {
  116. foreach ($data as $val) {
  117. echo sprintf("正在写入号码:\t\t %s", $val['format']) . PHP_EOL;
  118. $num = explode(" ", $val['format']);
  119. // $_num = str_replace(['+', ' '], ['', ''], $val);
  120. $config = Code::getData($num[0]);
  121. $insert = [
  122. 'country' => $num[0],
  123. 'country_name_zh' => $config['cn'],
  124. 'country_name_en' => $config['en'],
  125. 'phone' => $num[1],
  126. 'site' => 'smsfree.cc',
  127. 'url' => ""
  128. ];
  129. Num::insert($insert);
  130. }
  131. }
  132. }