This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathfunction.php
62 lines (58 loc) · 2.08 KB
/
function.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
<?PHP
/**
* +--------------------------------------------------------------
* | Copyright (c) 2016 DZLZH All rights reserved.
* +--------------------------------------------------------------
* | Author: DZLZH <[email protected]>
* +--------------------------------------------------------------
* | Filename: function.php
* +--------------------------------------------------------------
* | Last modified: 2016-11-15 20:57
* +--------------------------------------------------------------
* | Description:
* +--------------------------------------------------------------
*/
function curl_html($url, $userAgent = null, $cookie = null, $param = null, $file = null, $header = 0)
{
for ($i = 0; $i < 3; $i++) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($userAgent != null) {
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
}
if($param != null) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
}
if($cookie != null) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
if ($file != null) {
$fp = fopen($file, 'w');
curl_setopt($curl, CURLOPT_FILE, $fp);
}
$data = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$httpCodeIsMatched = preg_match('/4\d{2}|5\d{2}/', $info['http_code']);
if (!$httpCodeIsMatched) {
return $data;
}
usleep(1000000 * $i);
}
$error = 'Error::http_code:' . $info['http_code'] . ';url:' . $info['url'];
if (is_array($param)) {
$error .= ';param:' . '(' . serialize($param) . ')';
}
return $error;
}
//递归创建目录
function mk_dir($path)
{
if (is_dir($path)) {
return true;
}
return is_dir(dirname($path)) || mk_dir(dirname($path)) ? mkdir($path) : false;
}