SmsFree.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. return [
  86. 'from' => $a['from'],
  87. 'time' => date("Y-m-d H:i:s",intval($a['time'])),//date("Y-m-d H:i:s",a['time']),
  88. 'message' => $a['data'],
  89. ];
  90. }, $result['Message']);
  91. return $response;
  92. }
  93. private function insert($link, $data)
  94. {
  95. foreach ($data as $val) {
  96. echo sprintf("正在写入号码:\t\t %s", $val['format']) . PHP_EOL;
  97. $num = explode(" ", $val['format']);
  98. // $_num = str_replace(['+', ' '], ['', ''], $val);
  99. $config = Code::getData($num[0]);
  100. $insert = [
  101. 'country' => $num[0],
  102. 'country_name_zh' => $config['cn'],
  103. 'country_name_en' => $config['en'],
  104. 'phone' => $num[1],
  105. 'site' => 'smsfree.cc',
  106. 'url' => ""
  107. ];
  108. Num::insert($insert);
  109. }
  110. }
  111. }