123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- use PHPUnit\Framework\TestCase;
- use const mtopcard\ChinaMobileCard;
- use const mtopcard\ChinaTelecomCard;
- use const mtopcard\ChinaUnicomCard;
- use const mtopcard\ProvinceList;
- use const mtopcard\UnknownCard;
- define('APP_ID', 'test');
- define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/global.php');
- require_once(BASE_CORE_PATH . '/lrlz.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_CORE_PATH . '/framework/function/http.php');
- class TestCardType extends TestCase
- {
- const UnknownCard = 0;
- const ChinaMobileCard = 4; //手机卡
- const ChinaUnicomCard = 5; //手机卡
- const ChinaTelecomCard = 6; //手机卡
- public static function setUpBeforeClass(): void
- {
- Base::run_util();
- }
- #携号转网返回true,不是返回false,无法确定返回2.
- private function number_ported($card_no)
- {
- //用阿里的服务进行检测
- $ali_checker = function ($cardno)
- {
- if (empty($cardno)) return false; //手机号不能为空
- $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$cardno}";
- $resp = http_request($url); //获取API返回 的数据
- if(empty($resp)) return false;
- $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
- $datas = explode('=', $resp);
- if(count($datas) == 2)
- {
- $body = trim($datas[1]);
- if(preg_match_all("/(\w+):'([^']+)/", $body, $m))
- {
- $res = array_combine($m[1], $m[2]);
- if($res['catName'] == '中国联通') {
- return ChinaUnicomCard;
- }
- elseif($res['catName'] == '中国电信') {
- return ChinaTelecomCard;
- }
- elseif($res['catName'] == '中国移动') {
- return ChinaMobileCard;
- }
- else {
- return UnknownCard;
- }
- }
- }
- return UnknownCard;
- };
- #用正则表达式,以运营商的号段规则进行检查
- $rule_checker = function ($cardno)
- {
- 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)) {
- return ChinaMobileCard;
- } elseif (preg_match('/^(?:13[0-2]|145|15[56]|166|17[156]|18[56]|196)\d{8}$/', $cardno, $matches)) {
- return ChinaUnicomCard;
- } elseif (preg_match('/^(?:133|149|153|177|173|18[019]|19[0139])\d{8}$/', $cardno, $matches)) {
- return ChinaTelecomCard;
- } else {
- return UnknownCard;
- }
- };
- $ali_type = $ali_checker($card_no);
- $rule_type = $rule_checker($card_no);
- if($ali_type != UnknownCard && $rule_type != UnknownCard) {
- return ($ali_type != $rule_type);
- }
- else {
- return 2;
- }
- }
- public function testDiffISP()
- {
- $ret = $this->number_ported('13911129867');
- //如果ret 为true 表示携号转网,false 表示不是,2 表示不确定
- }
- }
|