TestCardType.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use function mtopcard\formatProvince;
  4. use const mtopcard\ChinaMobileCard;
  5. use const mtopcard\ChinaTelecomCard;
  6. use const mtopcard\ChinaUnicomCard;
  7. use const mtopcard\ProvinceList;
  8. use const mtopcard\UnknownCard;
  9. define('APP_ID', 'test');
  10. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  11. require_once(BASE_ROOT_PATH . '/global.php');
  12. require_once(BASE_CORE_PATH . '/lrlz.php');
  13. require_once(BASE_ROOT_PATH . '/fooder.php');
  14. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  15. class TestCardType extends TestCase
  16. {
  17. const UnknownCard = 0;
  18. const ChinaMobileCard = 4; //手机卡
  19. const ChinaUnicomCard = 5; //手机卡
  20. const ChinaTelecomCard = 6; //手机卡
  21. public static function setUpBeforeClass(): void
  22. {
  23. Base::run_util();
  24. }
  25. #携号转网返回true,不是返回false,无法确定返回2.
  26. private function number_ported($card_no)
  27. {
  28. //用阿里的服务进行检测
  29. $ali_checker = function ($cardno)
  30. {
  31. if (empty($cardno)) return false; //手机号不能为空
  32. $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$cardno}";
  33. $resp = http_request($url); //获取API返回 的数据
  34. if(empty($resp)) return false;
  35. $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
  36. $datas = explode('=', $resp);
  37. if(count($datas) == 2)
  38. {
  39. $body = trim($datas[1]);
  40. if(preg_match_all("/(\w+):'([^']+)/", $body, $m))
  41. {
  42. $res = array_combine($m[1], $m[2]);
  43. if($res['catName'] == '中国联通') {
  44. return ChinaUnicomCard;
  45. }
  46. elseif($res['catName'] == '中国电信') {
  47. return ChinaTelecomCard;
  48. }
  49. elseif($res['catName'] == '中国移动') {
  50. return ChinaMobileCard;
  51. }
  52. else {
  53. return UnknownCard;
  54. }
  55. }
  56. }
  57. return UnknownCard;
  58. };
  59. #用正则表达式,以运营商的号段规则进行检查
  60. $rule_checker = function ($cardno)
  61. {
  62. if (preg_match('/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|17[28]|18[2-478]|19[578])\d{8}$/', $cardno, $matches)) {
  63. return ChinaMobileCard;
  64. } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
  65. return ChinaUnicomCard;
  66. } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
  67. return ChinaTelecomCard;
  68. } else {
  69. return UnknownCard;
  70. }
  71. };
  72. $ali_type = $ali_checker($card_no);
  73. $rule_type = $rule_checker($card_no);
  74. if($ali_type != UnknownCard && $rule_type != UnknownCard) {
  75. return ($ali_type != $rule_type);
  76. }
  77. else {
  78. return 2;
  79. }
  80. }
  81. public function testDiffISP()
  82. {
  83. $ret = $this->number_ported('13911129867');
  84. //如果ret 为true 表示携号转网,false 表示不是,2 表示不确定
  85. }
  86. }