forked from neilopet/PHP-Filehost-Uploaders
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AbstractCurly.class.php
147 lines (137 loc) · 3.94 KB
/
AbstractCurly.class.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
<?php
/**
* AbstractCurly
*
* @author Neil Opet <neil.opet@gmail.com>
* @version 0.0.1
* @access public
*/
abstract class AbstractCurly
{
protected
$url,
$options,
$callback;
/**
* AbstractCurly::curly_init()
*
* @param string $url
* @return void
*/
function curly_init( $url )
{
if (!filter_var($url, FILTER_VALIDATE_URL))
die('URL not valid.');
$this->url = $url;
$this->options = array
(
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_NOPROGRESS => FALSE,
CURLOPT_TIMEOUT => 25,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1',
CURLOPT_HTTPHEADER => array('Expect:'),
CURLOPT_FRESH_CONNECT => TRUE
);
}
/**
* AbstractCurly::curly_exec()
*
* @return string $Response
*/
function curly_exec()
{
$ch = curl_init();
$callback = $this->callback;
# every request will have this callback
$curl = function($a, $b, $fileSize, $uploaded) use ($callback) {
$pct = 0;
if ($uploaded != 0)
{
$pct = floor(($uploaded / $fileSize) * 100);
call_user_func($callback, $pct);
}
};
$this->options[CURLOPT_PROGRESSFUNCTION] = $curl;
curl_setopt_array($ch, $this->options);
$ret = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
return $ret;
}
/**
* AbstractCurly::curly_post()
*
* @param mixed $postFields
* @param callback $callback
* @return string $Response
*/
function curly_post( $postFields, $callback )
{
$this->callback = $callback;
$this->options += array
(
CURLOPT_URL => $this->url,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $postFields,
CURLINFO_HEADER_OUT => TRUE,
);
return $this->curly_exec();
}
/**
* AbstractCurly::curly_put()
*
* @param mixed $postFields
* @param callback $callback
* @return string $Response
*/
function curly_put( $infile, $infilesize, $callback )
{
$this->callback = $callback;
$this->options += array
(
CURLOPT_URL => $this->url,
CURLOPT_PUT => TRUE,
CURLOPT_INFILE => $infile,
CURLOPT_INFILESIZE => $infilesize
);
return $this->curly_exec();
}
/**
* AbstractCurly::curly_get()
*
* @param array $getParams
* @param callback $callback
* @return string $Response
*/
function curly_get( array $getParams, $callback )
{
$this->callback = $callback;
$this->options += array
(
CURLOPT_URL => $this->url . '?' . http_build_query($getParams),
CURLOPT_POST => FALSE
);
return $this->curly_exec();
}
/**
* AbstractCurly::curly_ftp()
*
* @param string $infile
* @param string $infilesize
* @param callback $callback
* @return
*/
function curly_ftp( $infile, $infilesize, $callback )
{
$this->callback = $callback;
$this->options += array
(
CURLOPT_URL => $this->url,
CURLOPT_UPLOAD => TRUE,
CURLOPT_INFILE => $infile,
CURLOPT_INFILESIZE => $infilesize
);
return $this->curly_exec();
}
}