-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueryPhone.php
executable file
·60 lines (51 loc) · 1.29 KB
/
QueryPhone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Helper;
/**
* Created by PhpStorm.
* User: SD
* Date: 2016/7/6
* Time: 15:37
*/
//use HttpRequest;
class QueryPhone
{
/**
* 淘宝api
* https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=130********
*/
const PHONE_API = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm';
const BAIDU_API_PHONE = 'http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone';
const BAIDU_PAI_apikey = '14153e647b7aa96a4be61d8f90468e6c';
const QUERY_PHONE = 'PHONE:INFO:';
private static function _formatData($data)
{
$ret = null;
if (!empty($data)) {
preg_match_all("/(\w+):'([^']+)/", $data, $res);
$items = array_combine($res[1], $res[2]);
foreach ($items as $itemKey => $itemVal) {
$ret[$itemKey] = iconv('GB2312', 'UTF-8', $itemVal);
}
}
return $ret;
}
public static function verifyPhone($phone)
{
if (preg_match("/^1[34578]\d{9}/", $phone)) {
return true;
} else {
return false;
}
}
public static function query($phone)
{
$phoneData = null;
if (self::verifyPhone($phone)) {
//TODO 加缓存
$response = HttpRequest::request(self::PHONE_API, ['tel' => $phone]);
$phoneData = self::_formatData($response);
$phoneData['msg'] = '数据由阿里巴巴提供';
}
return $phoneData;
}
}