/
query.php
49 lines (41 loc) · 1.44 KB
/
query.php
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
<?php
require 'vendor/Parsedown.php';
function query($type){
if(!is_null($type)) {
//get parameter data
$parameters = Flight::request()->query->getData();
$cacheKey = $type.json_encode($parameters);
if(apc_exists($cacheKey)) {
echo apc_fetch($cacheKey);
} else {
$url = 'http://localhost:8080/sparql';
$query_string = file_get_contents('queries/' . $type . '.txt');
foreach ($parameters as $key => $value) {
$query_string = str_replace('{' . $key . '}', $value, $query_string);
}
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/sparql-query"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
apc_store($cacheKey,$result);
echo $result;
}
}
}
function sparql($type) {
$query_string = file_get_contents('queries/'.$type.'.txt');
echo $query_string;
}
function docs($type) {
$doc = file_get_contents('docs/'.$type.'.md');
echo Parsedown::instance()->text($doc);
}