-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyql_test.php
More file actions
80 lines (74 loc) · 3.31 KB
/
yql_test.php
File metadata and controls
80 lines (74 loc) · 3.31 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
<form id="geosearch">
<p><label for="query">Enter Location:</label>
<input id="query" type="text"/></p>
<p><input type="submit" value="Make Query"/></p>
</form>
<div id="results"></div>
<script>
/*
This example shows how to use YQL to
make queries to the GEO Web service.
The call to the YQL Web service uses
2-legged OAuth and is made with OpenSocial
functions.
*/
function makeQuery(e){
e.preventDefault(); // do not send off form
var container = document.getElementById('results');
var location = document.getElementById('query').value || 'SFO';
var content = '';
var BASE_URI = 'http://query.yahooapis.com/v1/yql';
// function calling the opensocial makerequest method
function runQuery(query, handler) {
gadgets.io.makeRequest(BASE_URI, handler, {
METHOD: 'POST',
POST_DATA: toQueryString({q: query, format: 'json'}),
CONTENT_TYPE: 'JSON',
AUTHORIZATION: 'OAuth'
});
};
// Tool function to create a request string
function toQueryString(obj) {
var parts = [];
for(var each in obj) if (obj.hasOwnProperty(each)) {
parts.push(encodeURIComponent(each) + '=' +
encodeURIComponent(obj[each]));
}
return parts.join('&');
};
// Run YQL query to GeoPlanet API and extract data from response
runQuery('select * from geo.places where text="' + location + '"',
function(rsp) {
if(rsp.data){
var place = rsp.data.query.results.place;
if(place[0]){
placeplace = place[0];
}
var name = place.name || 'Unknown';
var country = place.country.content || place[0].country.content ||
'Unknown';
var latitude = place.centroid.latitude || 'Unknown';
var longitude = place.centroid.longitude || 'Unknown';
var city = place.locality1.content || 'Unknown';
var state = place.admin1.content || 'Unknown';
var county = place.admin2.content || 'Unknown';
var zip = place.postal ? place.postal.content : 'Unknown';
content = '<ul><li><strong>Place Name: </strong>' + name + '</li>'+
'<li><strong>City/Town: </strong>' + city + '</li>' +
'<li><strong>County/District: </strong>' + county + '</li>' +
'<li><strong>State/Province: </strong>' + state + '</li>' +
'<li><strong>Zipcode: </strong>' + zip + '</li>' +
'<li><strong>Country: </strong>' + country + '</li>' +
'<li><strong>Latitude: </strong>' + latitude + '</li>' +
'<li><strong>Longitude: </strong>' + longitude + '</li></ul>';
container.innerHTML = content;
}
else {
container.innerHTML = gadgets.json.stringify(rsp);
}
});
}
// Create an event handler for submitting the form
var form = document.getElementById('geosearch');
form.addEventListener('submit',makeQuery,false);
</script>