Código de PHP - Clase wget

Imágen de perfil

Clase wgetgráfica de visualizaciones


PHP

estrellaestrellaestrellaestrellaestrella(1)
Actualizado el 06 de Noviembre del 2015 por Fernando (Creado el 30 de Octubre del 2015)
1.086 visualizaciones desde el 30 de Octubre del 2015. Una media de 21 por semana
Con esta clase de PHP se pueden realizar peticiones mediante Curl para obtener webs, realizar peticiones POST y otras muchas cosas.
http://lab.fawno.com/2015/05/17/clase-wget/

Requerimientos

PHP con Curl activado.

2015.08.27
estrellaestrellaestrellaestrellaestrella(1)

Actualizado el 06 de Noviembre del 2015 (Creado el 30 de Octubre del 2015)gráfica de visualizaciones de la versión: 2015.08.27
1.087 visualizaciones desde el 30 de Octubre del 2015. Una media de 21 por semana
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
 
	if (!defined('STDOUT')) {
		define('STDOUT', fopen('php://stdout', 'w'));
	}
 
	if (!function_exists('curl_strerror')) {
		function curl_strerror ($curl_error) {
			return $curl_error;
		}
	}
 
	if (!function_exists('curl_file_create')) {
		function curl_file_create ($filename, $mimetype = null, $postname = null) {
			$file = null;
			if (is_file($filename)) {
				if (empty($mimetype) and function_exists('mime_content_type')) $mimetype = mime_content_type($filename);
				if (empty($postname)) $postname = basename($filename);
				$file = '@';
				$file .= realpath($filename);
				$file .= ';filename=' . $postname;
				if (isset($mimetype)) $file .= ';type=' . $mimetype;
			}
			return $file;
		}
	}
 
	if (!function_exists('simplexml_import_html')) {
		function simplexml_import_html ($html) {
			$doc = new DOMDocument();
			@$doc->loadHTML($html);
			$xml = simplexml_import_dom($doc);
			return $xml;
		}
	}
 
	class wget {
		protected $curl = null;
		protected $cookie_jar = '';
		protected $headers = array();
		protected $response_headers = null;
 
		public function __construct ($useragent = null, $cookie_jar = '', $headers = null) {
			$this->curl = curl_init();
			$this->cookie_jar = $cookie_jar;
			if (isset($useragent)) curl_setopt($this->curl, CURLOPT_USERAGENT, $useragent);
			if (isset($headers) and is_array($headers)) array_walk($headers, function(&$item, $key) { if (!is_numeric($key)) $item = $key . ": " . $item; });
			if (isset($headers)) $this->headers = $headers;
			$this->set_header();
			curl_setopt($this->curl, CURLOPT_ENCODING, '');
			curl_setopt($this->curl, CURLOPT_PROGRESSFUNCTION, array($this, 'progress'));
			curl_setopt($this->curl, CURLOPT_FILETIME, true);
			curl_setopt($this->curl, CURLOPT_COOKIESESSION, true);
			$this->set_cookie_jar();
			curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array($this, 'read_header'));
		}
 
		public function __destruct () {
			curl_close($this->curl);
		}
 
		protected function read_header ($curl, $header) {
			$values = array_map('trim', explode(':', $header));
			if (isset($values[1])) {
				switch ($values[0]) {
					case 'Content-disposition':
					//case 'Content-Type':
						$values[1] = explode(';', $values[1]);
						foreach ($values[1] as $item => $value) {
							$value = explode('=', trim($value));
							if (isset($value[1])) {
								unset($values[1][$item]);
								$values[1][trim($value[0])] = trim($value[1]);
							}
						}
				}
				$this->response_headers[$values[0]] = $values[1];
			} elseif ($values[0]) {
				$this->response_headers[] = $values[0];
			}
			return strlen($header);
		}
 
		protected function progress () {
			static $url = null;
			static $progress_bar = null;
 
			$curl_info = curl_getinfo($this->curl);
			if ($url == $curl_info['url']) echo str_repeat("\x08", strlen($progress_bar));
 
			$url = $curl_info['url'];
			$progress_bar = null;
			if ($curl_info['http_code'] == 200) {
				$down_perc = 0;
				if ($curl_info['size_download'] <= $curl_info['download_content_length']) $down_perc = (100 * $curl_info['size_download']) / $curl_info['download_content_length'];
				$eta = 'N/A';
				if ($curl_info['speed_download']) $eta = ($curl_info['download_content_length'] - $curl_info['size_download']) / $curl_info['speed_download'];
				$progress_bar = sprintf('[%-30s] %3d%% %02.3fs %02.3fs', str_repeat('#', 30 * $down_perc / 100), $down_perc, $eta, $curl_info['total_time']);
			}
			echo $progress_bar, "\x20\x08";
		}
 
		public function curl_file_create ($filename, $mimetype = null, $postname = null) {
			$file = null;
			if (is_file($filename)) {
				if (empty($mimetype) and function_exists('mime_content_type')) $mimetype = mime_content_type($filename);
				if (empty($postname)) $postname = basename($filename);
				$file = curl_file_create($filename, $mimetype, $postname);
			}
			return $file;
		}
 
		public function set_auth ($user = null, $pass = null, $auth = null) {
			if (empty($auth)) $auth = CURLAUTH_ANY;
			curl_setopt($this->curl, CURLOPT_HTTPAUTH, $auth);
			curl_setopt($this->curl, CURLOPT_USERPWD, $user . ':' . $pass);
		}
 
		public function set_ssl_verifypeer ($ssl_verifypeer = false) {
			curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, $ssl_verifypeer);
		}
 
		public function set_verbose ($verbose = true) {
			curl_setopt($this->curl, CURLOPT_VERBOSE, (bool) $verbose);
		}
 
		public function set_followlocation ($follow = true) {
			curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, (bool) $follow);
		}
 
		public function set_maxredirs ($maxredirs = 0) {
			curl_setopt($this->curl, CURLOPT_MAXREDIRS, (int) $maxredirs);
		}
 
		public function set_header_out ($header_out = true) {
			curl_setopt($this->curl, CURLINFO_HEADER_OUT, (bool) $header_out);
		}
 
		public function set_header ($headers = null) {
			if (isset($headers) and is_array($headers)) array_walk($headers, function(&$item, $key) { if (!is_numeric($key)) $item = $key . ": " . $item; });
			if (empty($headers) and isset($this->headers)) $headers = $this->headers;
			curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
		}
 
		public function set_cookie ($cookie = null) {
			curl_setopt($this->curl, CURLOPT_COOKIE, $cookie);
		}
 
		public function set_cookie_jar ($cookie_jar = null) {
			if (empty($cookie_jar)) $cookie_jar = $this->cookie_jar;
			curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookie_jar);
			curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookie_jar);
		}
 
		public function info_url ($url, $referer = null) {
			$this->response_headers = null;
			curl_setopt($this->curl, CURLOPT_HTTPGET, true);
			curl_setopt($this->curl, CURLOPT_URL, $url);
			curl_setopt($this->curl, CURLOPT_REFERER, $referer);
			curl_setopt($this->curl, CURLOPT_NOPROGRESS, true);
			curl_setopt($this->curl, CURLOPT_NOBODY, true);
			curl_setopt($this->curl, CURLOPT_FILE, STDOUT);
			curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
			$body = curl_exec($this->curl);
			$curl_info = curl_getinfo($this->curl);
			$curl_info['body'] = $body;
			$curl_info['curl_errno'] = curl_errno($this->curl);
			$curl_info['curl_strerror'] = curl_strerror($curl_info['curl_errno']);
			$curl_info['headers'] = $this->response_headers;
			return $curl_info;
		}
 
		public function get_url ($url, $referer = null, $post_fields = null) {
			$this->response_headers = null;
			curl_setopt($this->curl, CURLOPT_POST, is_array($post_fields));
			curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post_fields);
			curl_setopt($this->curl, CURLOPT_HTTPGET, !is_array($post_fields));
			curl_setopt($this->curl, CURLOPT_URL, $url);
			curl_setopt($this->curl, CURLOPT_REFERER, $referer);
			curl_setopt($this->curl, CURLOPT_NOPROGRESS, true);
			curl_setopt($this->curl, CURLOPT_NOBODY, false);
			curl_setopt($this->curl, CURLOPT_FILE, STDOUT);
			curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
			$body = curl_exec($this->curl);
			$curl_info = curl_getinfo($this->curl);
			$curl_info['body'] = $body;
			$curl_info['curl_errno'] = curl_errno($this->curl);
			$curl_info['curl_strerror'] = curl_strerror($curl_info['curl_errno']);
			$curl_info['headers'] = $this->response_headers;
			return $curl_info;
		}
 
		public function get_file ($url, $filename = null, $referer = null, $progress = true) {
			$this->response_headers = null;
			if (empty($filename)) {
				$info = $this->info_url($url, $referer);
				if (isset($info['headers']['Content-disposition']['filename'])) {
					$filename = $info['headers']['Content-disposition']['filename'];
				}
			}
			if (empty($filename)) $filename = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
			if (!is_dir(dirname($filename))) mkdir(dirname($filename), 0775, true);
 
			$file_handle = fopen($filename, 'w');
 
			curl_setopt($this->curl, CURLOPT_HTTPGET, true);
			curl_setopt($this->curl, CURLOPT_URL, $url);
			curl_setopt($this->curl, CURLOPT_REFERER, $referer);
			curl_setopt($this->curl, CURLOPT_NOPROGRESS, (!$progress));
			curl_setopt($this->curl, CURLOPT_NOBODY, false);
			curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
			curl_setopt($this->curl, CURLOPT_FILE, $file_handle);
			curl_exec($this->curl);
 
			fclose($file_handle);
 
			$curl_info = curl_getinfo($this->curl);
			touch($filename, $curl_info['filetime']);
 
			$curl_info['filename'] = $filename;
			$curl_info['curl_errno'] = curl_errno($this->curl);
			$curl_info['curl_strerror'] = curl_strerror($curl_info['curl_errno']);
			$curl_info['headers'] = $this->response_headers;
			return $curl_info;
		}
 
	}
?>



Comentarios sobre la versión: 2015.08.27 (1)

Imágen de perfil
xve
01 de Noviembre del 2015
estrellaestrellaestrellaestrellaestrella
Muy interesante!!!
Responder

Comentar la versión: 2015.08.27

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios

http://lwp-l.com/s3324