-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
110 lines (86 loc) · 2.89 KB
/
search.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
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
<?php
/*
* Demo serverside file for Leaflet Search Plugin
* https://github.com/stefanocudini/leaflet-search
* https://bitbucket.org/zakis_/leaflet-search
*
* http://labs.easyblog.it/maps/leaflet-search
*
* Copyright 2013, Stefano Cudini - http://labs.easyblog.it/stefano-cudini/
* Licensed under the MIT license.
What's:
php code for testing jsonp and ajax features
receive get parameters:
q : search text
callback : callback name for jsonp request
Example Ajax:
request:
search.php?q=dark
response:
[{"loc":[41.34419,13.242145],"title":"darkblue"},{"loc":[41.67919,13.122145],"title":"darkred"}]
Example Jsonp:
request:
search.php?q=dark&callback=L.Control.Search.callJsonp
response:
L.Control.Search.callJsonp([{"loc":[41.34419,13.242145],"title":"darkblue"},{"loc":[41.67919,13.122145],"title":"darkred"}])
Example Bulk data:
request:
search.php?q=roma&cities=1
response:
[{"title":"Romainville","loc":[48.8854,2.43482]},{"title":"Roma","loc":[41.89474,12.4839]},{"title":"Roman","loc":[46.91667,26.91667]}]
Example Ajax Empty Result:
request:
search.php?q=xx
response:
{"ok":1,"results":[]}
Example Error Result:
request:
search.php?s=dark
response:
{"ok":0,"errmsg":"specify query parameter"}
*/
$url = 'http://mapnus-wsapi.cloudapp.net/api/search_api/search_svc/callback';
if (isDomainAvailible($url)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if(!isset($_GET['q']) or empty($_GET['q']))
die( json_encode(array('ok'=>0, 'errmsg'=>'specify q parameter') ) );
$json_obj = json_decode($data, true); //SIMULATE A DATABASE data
//the searched field is: title
}
function searchInit($text) //search initial text in titles
{
$reg = "/^".$_GET['q']."/i"; //initial case insensitive searching
return (bool)@preg_match($reg, $text['title']);
}
$fdata = array_filter($json_obj, 'searchInit'); //filter data
$fdata = array_values($fdata); //reset $fdata indexs
$JSON = json_encode($fdata,true);
if($_SERVER['REMOTE_ADDR'] == 'localhost') sleep(1);
//simulate connection latency for localhost tests
@header("Content-type: application/json; charset=utf-8");
if(isset($_GET['callback']) and !empty($_GET['callback'])) //support for JSONP request
echo $_GET['callback']."($JSON)";
else
echo $JSON; //AJAX request
function isDomainAvailible($domain) {
//check, if a valid url is provided
if(!filter_var($domain, FILTER_VALIDATE_URL)) {
return false;
}
//initialize curl
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($curlInit);
curl_close($curlInit);
if ($response) return true;
return false;
}
?>