-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysisAPI.php
More file actions
242 lines (204 loc) · 7.74 KB
/
AnalysisAPI.php
File metadata and controls
242 lines (204 loc) · 7.74 KB
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
<?php
/**
* Created by PhpStorm.
* User: QDFish
* Date: 2018/4/24
* Time: 上午9:13
*/
require_once "iOSModelVar.php";
require_once "iOSModelConstant.php";
class AnalysisAPI
{
private $api;
private $htmlSourceCode;
//域名
public $domain;
//数据模型名称
public $modelName;
//数据模型字典
public $jsonData;
//数据模型额外附属字典
public $jsonExtra;
//资源具体位置
public $detailURLPath;
//api描述
public $apiDesC;
//参数数组集
public $parameters = array();
public function __construct($api)
{
$this->api = $api;
$this->baseInit();
$this->curlexec();
$this->analysisHtml();
}
private function baseInit() {
global $modelURL;
global $domainsMap;
global $classMap;
$urlCompoents = explode(DIRECTORY_SEPARATOR, $modelURL);
$this->domain = $urlCompoents[3];
$modelName = $urlCompoents[count($urlCompoents) - 1];
$this->modelName = isset($domainsMap[$this->domain]['pre']) ? $domainsMap[$this->domain]['pre'] . ucfirst($modelName) : $modelName;
$classMap['data'] = ucfirst($this->modelName);
for ($i = 0; $i <= 3; $i++) {
unset($urlCompoents[$i]);
}
$this->detailURLPath = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $urlCompoents);
}
private function curlexec() {
global $modelURL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $modelURL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this->htmlSourceCode = curl_exec($ch);
$this->htmlSourceCode = self::subHtmlStr($this->htmlSourceCode, UpateTimeHtmlHeader);
curl_close($ch);
}
private function analysisHtml() {
//API描述
$this->apiDesC = self::subHtmlStr($this->htmlSourceCode, TitleHtmlHeader, TitleHtmlTail);
if ($this->apiDesC === false) {
echo "无法找到api描述" . PHP_EOL;
}
//想到相应的json标签和参数标签
$returnLab = ParameterHtml;
$jsonLab = JsonHtmlHeader;
if (strpos($this->htmlSourceCode, ParameterHtml) === false) {
$returnLab = ParameterHtml1;
if (strpos($this->htmlSourceCode, ParameterHtml1) === false) {
$returnLab = JsonHtmlHeader;
if (strpos($this->htmlSourceCode, JsonHtmlHeader) === false) {
$returnLab = PhpHtmlHeader;
$jsonLab = PhpHtmlHeader;
if (strpos($this->htmlSourceCode, PhpHtmlHeader) === false) {
echo '找不到相应参数标签' . PHP_EOL;
exit(1);
}
}
}
}
if (strpos($this->htmlSourceCode, JsonHtmlHeader) === false) {
$jsonLab = PhpHtmlHeader;
if (strpos($this->htmlSourceCode, PhpHtmlHeader) === false) {
echo '找不到相应json标签' . PHP_EOL;
exit(1);
}
}
//爬取参数数据
$parameterResult = self::subHtmlStr($this->htmlSourceCode, 0, $returnLab);
$parameterArr = explode(LiHtmlTail, $parameterResult);
foreach ($parameterArr as $parameterDesc) {
$parameter = self::subHtmlStr($parameterDesc, CodeHtmlHeader, CodeHtmlTail);
if ($parameter !== false) {
if ($parameter == 'page' || $parameter == 'limit') {
$this->parameters[] = [
'type' => "(int)",
'name' => $parameter
];
} else {
$this->parameters[] = [
'type' => "(NSString *)",
'name' => $parameter
];
}
}
}
//爬去json数据,取最多字符串数字最多为成功数据(野蛮爬)
$jsonDesc = $this->htmlSourceCode;
$jsons = array();
while (1) {
$json = self::subHtmlStr($jsonDesc, $jsonLab, JsonHtmlTail);
if ($json === false) {
break;
}
$jsons[] = $json;
$jsonDesc = self::subHtmlStr($jsonDesc, JsonHtmlTail, null);
}
$successJson = '';
foreach ($jsons as $value) {
if (strlen($value) > strlen($successJson)) {
$successJson = $value;
}
}
$jsonArr = json_decode($successJson, true);
$this->jsonData = ['data' => isset($jsonArr['info']['data']) ? $jsonArr['info']['data'] : null] ;
$this->jsonExtra = isset($jsonArr['info']['extra']) ? $jsonArr['info']['extra'] : null;
$this->jsonData = self::transformValueToType($this->jsonData);
$this->jsonExtra = self::transformValueToType($this->jsonExtra);
}
//header可传数值,当header或者tail搜索不到时,返回false,tail为null时,返回头部到tail的字段
public static function subHtmlStr($str, $header, $tail = null) {
if (is_integer($header)) {
$headerPos = $header;
} else {
$headerPos = strpos($str, $header);
if ($headerPos === false) {
return false;
} else {
$headerPos += strlen($header);
}
}
if ($tail === null) {
return substr($str, $headerPos);
}
$tailPos = strpos($str, $tail);
if ($tailPos === false) {
return false;
}
$strLenght = $tailPos - $headerPos;
$result = substr($str, $headerPos, $strLenght);
return $result;
}
//将字典中相关的数值转为相关的类型
private static function transformValueToType($dict) {
global $modelType, $classMap;
$newDict = count($dict) > 0 ? array() : null;
if ($newDict === null) {
return $newDict;
}
foreach ($dict as $key => $value) {
//一开始如果是数组,则只取第一部分,并把其它部分截取掉
if ($key === 0) {
$newDict = $value;
return self::transformValueToType($newDict);
break;
}
if (!is_string($value)) {
//有类映射并且是数值是数组类型
if (isset($classMap[$key]) && isset($value[0])) {
$newValue = $value[0];
$newKey = "NSArray<" . ucfirst($classMap[$key]) . $modelType[0] . "* > *";
$newDict[$key] = new IOSObject($newKey, self::transformValueToType($newValue));
break;
} else if (isset($classMap[$key]) && !isset($value[0]) ) {
$newValue = $dict[$key];
$newKey = ucfirst($classMap[$key]) . $modelType[0] . ' *';
$newDict[$key] = new IOSObject($newKey, self::transformValueToType($newValue));
} else if (!isset($classMap[$key]) && isset($value[0]) && is_string($value[0])){
$type = "NSArray<NSString* > *";
$newDict[$key] = $type;
} else {
self::transformValueToType(null);
}
} else if (strpos($key, 'is') !== false) {
$value = 'BOOL';
$newDict[$key] = $value;
} else {
$value = 'NSString *';
$newDict[$key] = $value;
}
}
return $newDict;
}
}
class IOSObject {
public $type;
public $value;
public function __construct($type, $value)
{
$this->type = $type;
$this->value = $value;
}
}