Browse Source

test tencent valid phone

xiaoyu 3 năm trước cách đây
mục cha
commit
181833a802
2 tập tin đã thay đổi với 108 bổ sung0 xóa
  1. 102 0
      helper/mtopcard/mtopcard.php
  2. 6 0
      test/TestCardNo.php

+ 102 - 0
helper/mtopcard/mtopcard.php

@@ -485,3 +485,105 @@ function is_validate($status)
         return true;
     }
 }
+
+
+function tencent_valid_phone($card_no)
+{
+    $type_checker = function ($channel)
+    {
+        if($channel == 'CUCC') {
+            return ChinaUnicomCard;
+        }
+        elseif($channel == 'CTCC') {
+            return ChinaTelecomCard;
+        }
+        elseif($channel == 'CMCC') {
+            return ChinaMobileCard;
+        }
+        else {
+            return UnknownCard;
+        }
+    };
+
+    $validate_checker = function ($code)
+    {
+        // 字段code:【1 为实号;2为空号;3为停机;4为流量卡;5为沉默号】
+        Log::record("status={$code}",Log::DEBUG);
+        if (in_array($code, [2])) {
+            return false;
+        } else {
+            return true;
+        }
+    };
+
+    $regin_checker = function ($province)
+    {
+        if(empty($province)) return -1;
+        $endtxts= ["省","市","自治区","特别行政区"];
+
+        foreach ($endtxts as $endtxt)
+        {
+            if(strpos($province, $endtxt) === false) {
+                continue;
+            } else {
+                $province = mb_strcut($province, 0, strrpos($province, $endtxt));
+            }
+        }
+
+        return array_search($province, ProvinceList);
+    };
+
+    $api_function = function ($card_no, &$card_type, &$validate, &$region_no,&$status) use($type_checker, $validate_checker, $regin_checker)
+    {
+        $secretId = 'AKIDkd6sT6C4765LtYN1W2fN2WC2Ynr42acemjQW';
+        $secretKey = 'dupQeU838dhuQ8uDP2bi81nQNb5wt9KW1qLP7eWu';
+        $source = 'market';
+
+        // 签名
+        $datetime = gmdate('D, d M Y H:i:s T');
+        $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
+        $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
+        $auth = sprintf('hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign);
+        $header = [
+            "X-Source: {$source}",
+            "X-Date: {$datetime}",
+            "Authorization: {$auth}",
+        ];
+        $data['number'] = $card_no;
+        $url = 'https://service-empgc6lp-1256724964.sh.apigw.tencentcs.com/release/query';
+        $net_err = 0;
+        $resp = http_request($url, $data, 'GET',false, $header,$net_err);
+
+        if ($resp == false) return false;
+
+        $resp = json_decode($resp, true);
+        if ($resp == false) return false;
+
+        if ($resp['ret'] == 200)
+        {
+            $data = $resp['data'];
+            $code = intval($data['code']);
+            $extend = $data['extend'];
+            $status = $code;
+            $card_type = $type_checker($extend['isp']);
+            $validate = $validate_checker($code);
+            $region_no = $regin_checker($extend['prov']);
+            return true;
+        } else {
+            Log::record("tencent_valid phone:{$card_no}", Log::DEBUG);
+            return false;
+        }
+    };
+
+    $validate = true;
+    $card_type = card_type($card_no,$region_no);
+    $region_no = -1;
+    $status = 6;
+
+    $ret = $api_function($card_no, $card_type, $validate, $region_no, $status);
+    if($ret)
+    {
+        return [$validate,$card_type,$region_no,$status];
+    }
+    return [$validate,$card_type,-1,$status];
+}

+ 6 - 0
test/TestCardNo.php

@@ -63,4 +63,10 @@ class TestCardNo extends TestCase
         $params['recharge_amount'] = number_format($price,4,'.','');
 
     }
+
+    public function testTencentValidPhone()
+    {
+        $card_no = 17801048874;
+        [$validate,$card_type,$region_no,$status] = mtopcard\tencent_valid_phone($card_no);
+    }
 }