1 | <?php |
---|
2 | /* |
---|
3 | * http_request class |
---|
4 | * |
---|
5 | * useful to avoid allow_url_fopen_wrapper issues |
---|
6 | * and to get or post data from anywhere we want |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | class http_request |
---|
11 | { |
---|
12 | function http_request($url = '', $method = 'GET') |
---|
13 | { |
---|
14 | if ($url) |
---|
15 | { |
---|
16 | $this->url = parse_url($url); |
---|
17 | } |
---|
18 | $this->useragent = 'Mozilla/4.0 (compatible)'; |
---|
19 | $this->method = $method; |
---|
20 | $this->postform = array(); |
---|
21 | $this->postdata = array(); |
---|
22 | $this->headers = array(); |
---|
23 | $this->cookiedata = array(); |
---|
24 | $this->socket_timeout = 5; |
---|
25 | } |
---|
26 | |
---|
27 | // socket timeout is the amount of time in second which is waited |
---|
28 | // for incoming data, set higher if you request stuff from heavy-loaded |
---|
29 | // scripts or compressed data streams |
---|
30 | function setSockettimeout($int) |
---|
31 | { |
---|
32 | $this->socket_timeout = $int; |
---|
33 | } |
---|
34 | |
---|
35 | function set_timeout($int) |
---|
36 | { |
---|
37 | $this->socket_timeout = $int; |
---|
38 | } |
---|
39 | |
---|
40 | function get_content() |
---|
41 | { |
---|
42 | $this->request(); |
---|
43 | return $this->content; |
---|
44 | } |
---|
45 | |
---|
46 | function get_header() |
---|
47 | { |
---|
48 | $this->request(); |
---|
49 | return $this->header; |
---|
50 | } |
---|
51 | |
---|
52 | function get_sent() |
---|
53 | { |
---|
54 | return $this->sent; |
---|
55 | } |
---|
56 | |
---|
57 | function get_recv() |
---|
58 | { |
---|
59 | return $this->recv; |
---|
60 | } |
---|
61 | |
---|
62 | function connect() |
---|
63 | { |
---|
64 | if ($this->fp) |
---|
65 | { |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | $this->fp = @fsockopen($this->url["host"], 80, $errno, $errstr, 5); |
---|
70 | if (!$this->fp) |
---|
71 | { |
---|
72 | $this->_errno = $errno; |
---|
73 | $this->_errstr = $errstr; |
---|
74 | return false; |
---|
75 | } |
---|
76 | return true; |
---|
77 | } |
---|
78 | |
---|
79 | function getError() |
---|
80 | { |
---|
81 | return 'Error occured with fsockopen: '.$this->_errstr.' ('.$this->_errno.')<br/>'."\n"; |
---|
82 | } |
---|
83 | |
---|
84 | function request() |
---|
85 | { |
---|
86 | if ($this->requested) |
---|
87 | { |
---|
88 | return; |
---|
89 | } |
---|
90 | $this->connect(); |
---|
91 | $fp = &$this->fp; |
---|
92 | |
---|
93 | if (!is_resource($fp)) |
---|
94 | { |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | // define a linefeed (carriage return + newline) |
---|
99 | $lf = "\r\n"; |
---|
100 | |
---|
101 | $request_string = $this->method.' '.$this->url['path'].'?'.$this->url['query'].' HTTP/1.0'.$lf |
---|
102 | .'Accept-Language: en'.$lf |
---|
103 | .'User-Agent: '.$this->useragent.$lf |
---|
104 | .'Host: '.$this->url['host'].$lf |
---|
105 | .'Connection: close'.$lf; |
---|
106 | if (0 &&count($this->url['user']) && count($this->url['pass'])) |
---|
107 | { |
---|
108 | $base64 = base64_encode($this->url['user'].':'.$this->url['pass']); |
---|
109 | $request_string .= 'Authorization: Basic '.$base64.$lf.$lf; |
---|
110 | } |
---|
111 | if (count($this->headers)) |
---|
112 | { |
---|
113 | $request_string .= join($lf, $this->headers).$lf; |
---|
114 | } |
---|
115 | if (count($this->cookiedata)) |
---|
116 | { |
---|
117 | $request_string .= 'Cookie: '; |
---|
118 | foreach ($this->cookiedata as $key => $value) |
---|
119 | { |
---|
120 | $request_string .= $key.'='.$value.'; '; |
---|
121 | } |
---|
122 | $request_string .= $lf; |
---|
123 | } |
---|
124 | if ($this->method == 'POST') |
---|
125 | { |
---|
126 | $boundary = substr(md5(rand(0,32000)),0,10); |
---|
127 | $data = '--'.$boundary.$lf; |
---|
128 | |
---|
129 | foreach ($this->postform as $name => $content_file) |
---|
130 | { |
---|
131 | $data .= 'Content-Disposition: form-data; name="'.$name.'"'.$lf.$lf; |
---|
132 | $data .= $content_file.$lf; |
---|
133 | $data .= '--'.$boundary.$lf; |
---|
134 | } |
---|
135 | |
---|
136 | foreach ($this->postdata as $name => $content_file) |
---|
137 | { |
---|
138 | $data .= 'Content-Disposition: form-data; name="'.$name.'"; filename="'.$name.'"'.$lf; |
---|
139 | $data .= 'Content-Type: text/plain'.$lf.$lf; |
---|
140 | $data .= $content_file.$lf; |
---|
141 | $data .= '--'.$boundary.$lf; |
---|
142 | } |
---|
143 | |
---|
144 | $request_string .= 'Content-Length: '.strlen($data).$lf; |
---|
145 | $request_string .= 'Content-Type: multipart/form-data, boundary='.$boundary.$lf; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | $data = ''; |
---|
150 | } |
---|
151 | $request_string .= $lf; |
---|
152 | |
---|
153 | fputs($fp, $request_string.$data); |
---|
154 | $this->sent = strlen($header)+strlen($data); |
---|
155 | |
---|
156 | $header = 1; |
---|
157 | socket_set_timeout($fp, $this->socket_timeout); |
---|
158 | while ($line = fgets($fp, 4096)) |
---|
159 | { |
---|
160 | if ($header) |
---|
161 | { |
---|
162 | $http_header .= $line; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | $file .= $line; |
---|
167 | } |
---|
168 | if ($header && $line == $lf) |
---|
169 | { |
---|
170 | $header = 0; |
---|
171 | } |
---|
172 | } |
---|
173 | $this->status = socket_get_status($fp); |
---|
174 | fclose($fp); |
---|
175 | $this->header = $http_header; |
---|
176 | $this->content = $file; |
---|
177 | $this->recv = strlen($http_header)+strlen($file); |
---|
178 | $this->requested = true; |
---|
179 | } |
---|
180 | |
---|
181 | function url($url) |
---|
182 | { |
---|
183 | $this->url = parse_url($url); |
---|
184 | } |
---|
185 | |
---|
186 | // this is to send file-data to be accessed with $_FILES[$name] |
---|
187 | function set_postdata($name, $data) |
---|
188 | { |
---|
189 | $this->method = 'POST'; |
---|
190 | $this->postdata[$name] = $data; |
---|
191 | } |
---|
192 | |
---|
193 | // this function sends form data objects like $_POST[$name] = $data |
---|
194 | function set_postform($name, $data) |
---|
195 | { |
---|
196 | $this->method = 'POST'; |
---|
197 | $this->postform[$name] = $data; |
---|
198 | } |
---|
199 | |
---|
200 | function set_cookie($name, $data) |
---|
201 | { |
---|
202 | $this->cookiedata[$name] = $data; |
---|
203 | } |
---|
204 | |
---|
205 | function set_useragent($string) |
---|
206 | { |
---|
207 | $this->useragent = $string; |
---|
208 | } |
---|
209 | |
---|
210 | function set_header($headerstring) |
---|
211 | { |
---|
212 | if (!strpos($headerstring, ':')) |
---|
213 | { |
---|
214 | return; |
---|
215 | } |
---|
216 | $this->headers[$headerstring] = $headerstring; |
---|
217 | } |
---|
218 | } |
---|
219 | ?> |
---|