ToolRepository.php
6.6 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
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/10/26
* Time: 13:55
*/
namespace App\Repositories;
/**
* Class ToolRepository
* @package App\Repositories
*/
class ToolRepository
{
/**
* @param $url
* @param $data
* @param string $method
* @param array $header
* @param int $time_out
* @return array
*/
public function curlRequest($url, $data, $method = 'POST', $header = [], $time_out = 60)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if ($data)
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
'Expect:',
'Content-type: application/json',
'Accept: application/json',
], $header)
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [$code, $response];
}
/**
* 多请求批处理
* @param $host
* @param $request_urls
* @return array
*/
public function batCurlblobr($request_urls)
{
// //脚本开始的毫秒时刻
// $start = microtime(true);
//打开一个curl批处理句柄
$mh = curl_multi_init();
$headers = [
"X-BLOBR-KEY:" . $this->key
];
$ch = [];
foreach ($request_urls as $key => $url) {
//初始化cURL会话
$ch[$key] = curl_init($url);
//设置curl传输选项
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$key], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch[$key], CURLOPT_ENCODING, "");
curl_setopt($ch[$key], CURLOPT_MAXREDIRS, 10);
// curl_setopt($ch[$key], CURLOPT_TIMEOUT, $this->time_out);
curl_setopt($ch[$key], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch[$key], CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch[$key], CURLOPT_HTTPHEADER, $headers);
// curl_setopt($ch[$key], CURLOPT_HEADER, 0);
//关闭https请求验证
// if (strpos($url,'https')){
// curl_setopt ( $ch[$key], CURLOPT_SSL_VERIFYPEER, false );
// curl_setopt ( $ch[$key], CURLOPT_SSL_VERIFYHOST, 2 );
// }
//向批处理句柄中添加单独的curl句柄
curl_multi_add_handle($mh, $ch[$key]);
}
$running = null;
//执行批处理句柄
do {
$mrc = curl_multi_exec($mh, $running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($running && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
$res = [];
//获取内容
foreach ($request_urls as $k => $url) {
//关闭执行完的子句柄
curl_multi_remove_handle($mh, $ch[$k]);
//返回获取的输出文本流
$res[$k] = curl_multi_getcontent($ch[$k]);
}
//5.关闭父curl
curl_multi_close($mh);
// $end = microtime(true) - $start;
// file_put_contents(__DIR__ . '/exec_time.log', $end . PHP_EOL, FILE_APPEND);
return $res;
}
/**
* 多请求批处理
* @param $headers
* @param $request_urls
* @return array
*/
public function batCurl($request_urls, $headers = [])
{
// //脚本开始的毫秒时刻
// $start = microtime(true);
//打开一个curl批处理句柄
$mh = curl_multi_init();
$ch = [];
foreach ($request_urls as $key => $url) {
//初始化cURL会话
$ch[$key] = curl_init($url);
//设置curl传输选项
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$key], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch[$key], CURLOPT_ENCODING, "");
curl_setopt($ch[$key], CURLOPT_MAXREDIRS, 10);
curl_setopt($ch[$key], CURLOPT_TIMEOUT, 30);
curl_setopt($ch[$key], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch[$key], CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch[$key], CURLOPT_HTTPHEADER, $headers);
// curl_setopt($ch[$key], CURLOPT_HEADER, 0);
//关闭https请求验证
// if (strpos($url,'https')){
// curl_setopt ( $ch[$key], CURLOPT_SSL_VERIFYPEER, false );
// curl_setopt ( $ch[$key], CURLOPT_SSL_VERIFYHOST, 2 );
// }
//向批处理句柄中添加单独的curl句柄
curl_multi_add_handle($mh, $ch[$key]);
}
$running = null;
//执行批处理句柄
do {
$mrc = curl_multi_exec($mh, $running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($running && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $running);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
$res = [];
//获取内容
foreach ($request_urls as $k => $url) {
//关闭执行完的子句柄
curl_multi_remove_handle($mh, $ch[$k]);
//返回获取的输出文本流
$res[$k] = curl_multi_getcontent($ch[$k]);
}
//5.关闭父curl
curl_multi_close($mh);
// $end = microtime(true) - $start;
// file_put_contents(__DIR__ . '/exec_time.log', $end . PHP_EOL, FILE_APPEND);
return $res;
}
/**
* 替换特殊词汇
* @param $data
* @return array
*/
public function filterString($data)
{
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = $this->filterString($val);
}
} else {
$data = ' ' . $data;
$array = "/\(\w.*\)| company| Company| inc| Inc| Co| co| Ltd| ltd| Llc| llc| Import And Export| Limited| limited|,|\./";
$result = preg_replace($array, '', $data);
return $result == ' ' ? '' : $result;
}
return $data;
}
}