-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsingleApp.php
More file actions
executable file
·162 lines (136 loc) · 6.18 KB
/
singleApp.php
File metadata and controls
executable file
·162 lines (136 loc) · 6.18 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
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
<?php
//edge cases (errors):
//http://localhost:8024/RAE-API/search/amigo
//"key" param now recognizes utf8 strings, then re-decode isn't mandatory
//$word = utf8_decode( ( isset( $_GET["search"] ) ? mb_strtolower( $_GET["search"], 'UTF-8' ) : "" ) );
$word = ( isset( $_GET["search"] ) ? mb_strtolower( $_GET["search"], 'UTF-8' ) : "" );
$query = array(
'origen' => 'RAE' ,
'type' => '3' ,
'val_aux' => '' ,
'key' => $word
);
$endpoint = 'http://lema.rae.es/drae/srv/';
$queryURL = $endpoint . "search?" . http_build_query( $query );
//Todo: These fields could be obtained from the first page query, hence, this part of the word/meaning phase it could be automatized.
$fields = array(
'TS014dfc77_id' => urlencode( "3" ),
'TS014dfc77_cr' => '0062b2344bb9411eefe2a2de74bf702b:deec:hf74lCd7:2024784252',
'TS014dfc77_76' => urlencode( "0" ),
'TS014dfc77_86' => urlencode( "0" ),
'TS014dfc77_md' => urlencode( "1" ),
'TS014dfc77_rf' => urlencode( "0" ),
'TS014dfc77_ct' => urlencode( "0" ),
'TS014dfc77_pd' => urlencode( "0" )
);
$fields_string = http_build_query( $fields );
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL , $queryURL );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST , count( $query ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS , $fields_string );
//execute post
$result = curl_exec( $ch );
//close connection
curl_close( $ch );
try
{
$doc = new DOMDocument();
@$doc->loadHTML( '<?xml encoding="utf-8" ?>' . $result );
$doc->saveHTML();
$xpath = new DOMXpath($doc);
$lemas = $xpath->query( "/html/body/div" );
$entity = array();
$defs = array();
if( $lemas->length > 0 )
{
for( $index = 1; $index <= $lemas->length; $index++ )
{
$defNodes = $xpath->query( "/html/body/div[{$index}]/p[position()>3]" );
$definiciones = call_user_func( function() use( $defNodes )
{
$i = 0;
$ex = false;
foreach( $defNodes as $def )
{
$definicion = trim( preg_replace("/(\d+.)/", "", $def->textContent ) );
if( $def->getAttribute( "class" ) == "p" )
{
$defs[] = array( trim( $definicion ) => array() );
$carry = $i;
$carry2 = trim( $definicion );
$ex = true;
//var_dump($i." - ".$carry." - ".$carry2."<br>");
}
elseif( $def->getAttribute( "class" ) == "q" and $ex )
{
// var_dump($i." - ".$carry." - ".$carry2."<br>", $defs );
$defs[ $carry ][ $carry2 ][] = trim( $definicion );
}
else
{
$defs[] = trim( $definicion );
}
if( ( $def->getAttribute( "class" ) == "q" and !$ex ) OR $def->getAttribute( "class" ) == "p" OR $def->getAttribute( "class" ) == "m" )
{
$i++;
}
}
return $defs;
});
$entity[] = array(
"etimología" => ( $xpath->query( "/html/body/div[{$index}]/p[2]" )->length > 0 ? trim( $xpath->query( "/html/body/div[{$index}]/p" )->item( 1 )->textContent ) : NULL ) ,
"id" => ( $xpath->query( "/html/body/div[{$index}]/a" ) ->length > 0 ? $xpath->query( "/html/body/div[{$index}]/a" )->item( 0 ) ->getAttribute("name") : NULL ) ,
"lema" => $xpath ->query("/html/body/div[{$index}]/p") ->item( 0 )->textContent ,
"definiciones" => $definiciones
);
}
}
else
{
$altNodes = $xpath->query( "/html/body/ul/li/a" );
if( $altNodes->length > 0 )
{
$alternativas = call_user_func( function() use( $altNodes, $endpoint )
{
foreach( $altNodes as $alt )
{
$definicion = trim( $alt->textContent );
$defs[] = array( $definicion => $endpoint . $alt->getAttribute( "href" ) );
}
return $defs;
});
$mensaje = ( isset( $xpath->query( "/html/body/p/span" )->item(0)->textContent ) ? $xpath->query( "/html/body/p/span" )->item(0)->textContent : false );
$entity = array(
"error" => TRUE,
"mensaje" => $mensaje,
"alternativas" => $alternativas
);
}
else
{
$mensaje = $xpath->query( "/html/body/p/font" );
if( $mensaje->length > 0 )
{
$entity = array(
"error" => TRUE,
"mensaje" => $mensaje->item(0)->textContent
);
}
else
{
$entity = array(
"error" => TRUE,
"mensaje" => "Error desconocido"
);
}
}
}
header( "Content-Type: application/json" );
print( json_encode( $entity ) );
}
catch( Exception $e )
{
}