TestCardType.php 3.3 KB

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