-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
63 lines (52 loc) · 1.74 KB
/
functions.php
File metadata and controls
63 lines (52 loc) · 1.74 KB
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
<?php
declare(strict_types=1);
/**
* Sanitiza contenido para dispositivos móviles antiguos
*/
function mobile_cleanup(string $input, string $encoding): string {
$cleaned = strip_tags($input);
$cleaned = html_entity_decode($cleaned, ENT_QUOTES | ENT_HTML5, $encoding);
return htmlspecialchars($cleaned, ENT_XML1 | ENT_QUOTES, $encoding);
}
/**
* Obtiene y parsea un feed XML con manejo robusto de errores
*/
function curl_get_xml(string $url, string $encoding): SimpleXMLElement {
$ch = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3,
CURLOPT_TIMEOUT => 15,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; XMLConverter/2.0)',
CURLOPT_HTTPHEADER => [
"Accept-Charset: $encoding",
"Accept: application/xml, text/xml;q=0.9,*/*;q=0.8"
]
];
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new RuntimeException('Error CURL: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new RuntimeException("Error HTTP: $httpCode");
}
curl_close($ch);
libxml_use_internal_errors(true);
$xml = simplexml_load_string((string)$response);
if ($xml === false) {
$errors = implode("\n", array_map(
fn($error) => $error->message,
libxml_get_errors()
));
libxml_clear_errors();
throw new RuntimeException("Errores XML:\n$errors");
}
return $xml;
}