forked from foxiswho/taobao-area-php
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Areas.php
243 lines (201 loc) · 7.84 KB
/
Areas.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* @noinspection SqlResolve
* @noinspection SqlNoDataSourceInspection
* @noinspection SqlDialectInspection
*/
class Areas {
const AREAS_JSON_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'area.json';
const OUTPUT_FILE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'area.sql';
const GET_AREAS_URL = 'https://gw.api.tbsandbox.com/router/rest';
const GET_TOWNS_URL = 'https://lsp.wuliu.taobao.com/locationservice/addr/output_address_town_array.do';
const VALUE_SQL = 'INSERT INTO `area` VALUES ';
const TABLE_SQL = <<<EOF
DROP TABLE IF EXISTS `area`;
CREATE TABLE `area` (
`id` INT(10) UNSIGNED NOT NULL,
`type` VARCHAR(8) NOT NULL COMMENT '区域类型, 1 - 国家;2 - 省/自治区/直辖市;3 - 地级市/辖区;4 - 县/县级市/区;5 - 街道/乡镇',
`name` VARCHAR(64) NOT NULL COMMENT '地域名称',
`parent_id` INT(10) UNSIGNED DEFAULT '0' COMMENT '父节点区域标识',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='地域表';
EOF;
private $secret = 'test';
private $requestParams = [
'app_key' => 'test',
'fields' => 'id,type,name,parent_id',
'force_sensitive_param_fuzzy' => 'true',
'format' => 'json',
'method' => 'taobao.areas.get',
'partner_id' => 'top-apitools',
'sign_method' => 'md5',
'v' => '2.0',
];
private $ch;
private $getTowns = false;
private $countries;
private $provinces;
private $cities;
private $districts;
private $abroad;
public function __construct() {
$this->requestParams['timestamp'] = date('Y-m-d H:i:s');
$this->ch = curl_init();
curl_setopt_array($this->ch, [
CURLOPT_AUTOREFERER => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_CONNECTTIMEOUT => 10
]);
}
public function needTowns($need = false) {
$this->getTowns = $need;
return $this;
}
public function generate() {
if ($this->getCountryProvinceCityDistrict()->makeSqlFile()) {
echo "生成结束,SQL 文件位于 output 目录内\n";
}
}
private function getSign($params) {
ksort($params, SORT_STRING);
$paramsStr = '';
foreach ($params as $key => $param) {
$paramsStr .= $key . $param;
}
return strtoupper(md5($this->secret . $paramsStr . $this->secret));
}
/**
* @return bool|string
*/
private function getAreasData() {
if (file_exists(self::AREAS_JSON_FILE)) {
// file size
if (filesize(self::AREAS_JSON_FILE) / 1024 > 256) {
return file_get_contents(self::AREAS_JSON_FILE);
}
}
$this->requestParams['sign'] = $this->getSign($this->requestParams);
$url = self::GET_AREAS_URL . '?' . http_build_query($this->requestParams);
echo "获取基本区域数据...\n请求地址:{$url}\n";
$jsonFile = file_get_contents($url);
file_put_contents(self::AREAS_JSON_FILE, $jsonFile);
return $jsonFile;
}
private function getCountryProvinceCityDistrict() {
$areasData = $this->getAreasData();
if ($areasData) {
$areasData = function_exists('jsond_decode')
? jsond_decode($areasData, true)
: json_decode($areasData, true);
if (isset($areasData['areas_get_response'])) {
foreach ($areasData['areas_get_response']['areas'] as $areas) {
foreach ($areas as $area) {
switch ($area['type']) {
case 1:
$this->countries[] = $area;
break;
case 2:
$this->provinces[] = $area;
break;
case 3:
$this->cities[] = $area;
break;
case 4:
$this->districts[] = $area;
break;
default:
$this->abroad[] = $area;
}
}
}
}
}
return $this;
}
private function appendSqlValues($file_path, $areas, $last = false) {
$lastKey = count($areas) - 1;
foreach ($areas as $key => $area) {
$data = "({$area['id']},{$area['type']},'{$area['name']}',{$area['parent_id']}),";
if ($last && $key === $lastKey) {
$data = rtrim($data, ',');
}
if (file_put_contents($file_path, $data, FILE_APPEND) === false) {
unlink($file_path);
die("追写文件失败,请重试\n");
}
}
}
private function curlRequest($url) {
curl_setopt($this->ch, CURLOPT_URL, $url);
return curl_exec($this->ch);
}
private function getTowns() {
$ret = [];
foreach ($this->districts as $district) {
$url = self::GET_TOWNS_URL . "?l1={$district['parent_id']}&l2={$district['id']}";
for ($i = 0; $i < 3; $i++) {
$towns = $this->curlRequest($url);
if ($towns && stripos($towns, 'callback({success:true,result') !== false) {
break;
}
usleep(500);
continue;
}
if (isset($towns) && $towns) {
$towns = json_decode(str_replace("'", '"', substr($towns, 30, -3)), true);
foreach ($towns as $town) {
$ret[] = [
'id' => $town[0],
'type' => 5,
'name' => $town[1],
'parent_id' => $town[2]
];
}
} else {
echo "该地址:{$url},请求三次失败,请手动添加该数据\n";
}
}
return $ret;
}
/**
* @return bool
*/
private function makeSqlFile() {
if (empty($this->countries)) {
exit('数据为空,无法生成文件');
}
if (file_put_contents(self::OUTPUT_FILE_PATH, self::TABLE_SQL) === false) {
exit('写入文件失败,请检查权限');
}
file_put_contents(self::OUTPUT_FILE_PATH, self::VALUE_SQL, FILE_APPEND);
echo "----- 生成国家数据 ------\n";
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $this->countries);
echo "----- 生成省份数据 ------\n";
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $this->provinces);
echo "----- 生成省市数据 ------\n";
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $this->cities);
echo "----- 生成市区数据 ------\n";
if ($this->getTowns) {
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $this->districts);
$begin = date('Y-m-d H:i:s');
echo "----- 生成街道数据开始,时间:{$begin} ------\n";
ini_set('memory_limit', '512M');
$towns = $this->getTowns();
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $towns, true);
$end = date('Y-m-d H:i:s');
echo "----- 生成街道数据结束,时间:{$end} ------\n";
} else {
$this->appendSqlValues(self::OUTPUT_FILE_PATH, $this->districts, true);
}
file_put_contents(self::OUTPUT_FILE_PATH, ';', FILE_APPEND);
return true;
}
public function __destruct() {
curl_close($this->ch);
}
}