-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurl.php
47 lines (39 loc) · 1.34 KB
/
curl.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
<?php
function request($url, $method = 'GET', string $data = '', $headers = [])
{
$method = strtoupper($method);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$method === 'POST' && curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
empty($headers) || curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/******* add option *******/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, '/tmp/fastcar.sock');
/*************************/
$t1 = microtime(true);
$response = curl_exec($ch);
$delay = microtime(true) - $t1;
// 检查是否有错误发生
if (curl_errno($ch)) {
return [
'error' => curl_error($ch)
];
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status' => $httpCode,
'response' => $response,
'delay' => round($delay * 1000, 2),
];
}
for ($i = 0; $i < 100; $i++) {
sleep(1);
// $res = request('http://api.fanyi.baidu.com/api/trans/vip/translate');
$res = request('https://api.fanyi.baidu.com/api/trans/vip/translate');
$delay = $res['delay'];
echo $delay . PHP_EOL;
}