-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.php
More file actions
executable file
·117 lines (106 loc) · 2.87 KB
/
Copy pathws.php
File metadata and controls
executable file
·117 lines (106 loc) · 2.87 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
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
<?php
require 'lib/library.php';
// Recoger parámetros de entrada. Se permite GET y POST
$paramOpcion = $_REQUEST['opcion'];
$paramLista = $_REQUEST['listaParametros'];
$formato = strtolower($_REQUEST['format']) == 'xml' ? 'xml' : 'json';
// Obtener consulta SQL a ejecutar
$sql = getSQL($paramOpcion);
$sql = aplicarParametrosSQL($sql,$paramLista);
// Lanzar consulta a la base de datos
if ($sql == '')
die ('Opcion incorrecta');
else
{
$datos = getArrayDatos($sql);
echo formatData($datos,$formato);
}
// Dado un nombre de opción, buscar en el xml su SQL correspondiente
function getSQL($nombreOpcion)
{
$xmlOpciones = simplexml_load_file('opciones.xml');
$sql = "";
foreach ($xmlOpciones->opcion as $opcion)
{
$nombre = $opcion->nombre;
if ($nombre == $nombreOpcion)
{
$sql = $opcion->SQL;
break;
}
}
return $sql;
}
// Aplicar parámetros de la sentencia SQL
function aplicarParametrosSQL($sql, $listaPametros)
{
if ($listaPametros != '')
{
$arrParam = explode('#',$listaPametros);
for ($i=0; $i<count($arrParam); $i++)
{
$nomParam = '#PARAMETRO_'.($i+1).'#';
$valor = $arrParam[$i];
$sql = str_replace($nomParam, $valor, $sql);
}
}
return $sql;
}
function getArrayDatos($sql)
{
// $db = mysql_connect('127.0.0.1','root','root') or die('No se pudo conectar con la base de datos');
// mysql_select_db('jhpruebas',$db) or die('No se pudo seleccionar la base de datos.');
//$resul = mysql_query($sql,$db) or die('Consulta incorrecta: '.$sql);
$resul = mysql_query($sql) or die('Consulta incorrecta: '.$sql);
/* create one master array of the records */
$datos = array();
if(mysql_num_rows($resul))
{
while($row = mysql_fetch_assoc($resul))
{
$datos[] = $row;
}
}
/* disconnect from the db */
@mysql_close($db);
/* Return data array */
return $datos;
}
function formatData($data,$format='json')
{
if($format == 'json' || $format == 'jsonp')
{
header('Content-type: application/json');
if (isset($_REQUEST['callback'])) // Peticiones ajax
return $_REQUEST['callback'].'('.json_encode($data).')';
else
return json_encode($data);
}
else
{
header('Content-type: text/xml');
$xml = new SimpleXMLElement('<response/>');
return array2xml($data, $xml);
}
}
function array2xml($array, $xml = false)
{
if($xml === false)
{
$xml = new SimpleXMLElement('<root/>');
}
foreach($array as $key => $value)
{
if(is_array($value))
{
//array2xml($value, $xml->addChild($key));
array2xml($value, $xml->addChild('item'));
}
else
{
$xml->addChild($key, htmlspecialchars($value));
}
}
return $xml->asXML();
}
?>