1
- <h2>Using YQL to Access the Upcoming API</h2>
2
- <form name='upcoming_form'>
3
- Location: <input name='___location' id='___location' type='text' size='20'/><br/>
4
- Event: <input name='event' id='event' type='text' size='20'/><br/>
5
- <p><button id='find_event'>Find Event</button></p>
1
+ <form id =" geosearch" >
2
+ <p ><label for = " query" >Enter Location:</label >
3
+ <input id = " query" type = " text" /></p >
4
+ <p ><input type = " submit" value = " Make Query" /></p >
6
5
</form >
6
+ <div id = " results" ></div >
7
7
8
8
<script >
9
- // Attach event handler to button
10
- document.getElementById("find_event").addEventListener("click",find_event,false);
11
- // Get user input and submit form
12
- function find_event(){
13
- document.upcoming_form.event.value = document.getElementById('event').value || "music";
14
- document.upcoming_form.___location.value = document.getElementById('___location').value || "San Francisco";
15
- document.upcoming_form.submit();
16
- }
17
- </script>
18
- <?php
19
- $ BASE_URL = "https://query.yahooapis.com/v1/public/yql " ;
9
+ /*
10
+ This example shows how to use YQL to
11
+ make queries to the GEO Web service.
12
+ The call to the YQL Web service uses
13
+ 2-legged OAuth and is made with OpenSocial
14
+ functions.
15
+ */
16
+ function makeQuery(e){
17
+ e . preventDefault (); // do not send off form
18
+ var container = document . getElementById (' results' );
19
+ var ___location = document . getElementById (' query' ). value || ' SFO' ;
20
+ var content = ' ' ;
20
21
21
- if (isset ($ _GET ['event ' ]) && isset ($ _GET ['___location ' ])){
22
- $ ___location = $ _GET ['___location ' ];
23
- $ query = $ _GET ['event ' ];
24
- $ events ="" ;
25
-
26
- // Form YQL query and build URI to YQL Web service
27
- $ yql_query = "select * from upcoming.events where ___location=' $ ___location' and search_text=' $ query' " ;
28
- $ yql_query_url = $ BASE_URL . "?q= " . urlencode ($ yql_query ) . "&format=json " ;
22
+ var BASE_URI = ' http://query.yahooapis.com/v1/yql' ;
29
23
30
- // Make call with cURL
31
- $ session = curl_init ($ yql_query_url );
32
- curl_setopt ($ session , CURLOPT_RETURNTRANSFER ,true );
33
- $ json = curl_exec ($ session );
34
- // Convert JSON to PHP object
35
- $ phpObj = json_decode ($ json );
36
-
37
- // Confirm that results were returned before parsing
38
- if (!is_null ($ phpObj ->query ->results )){
39
- // Parse results and extract data to display
40
- foreach ($ phpObj ->query ->results ->event as $ event ){
41
- $ events .= "<div><h2> " . $ event ->name . "</h2><p> " ;
42
- $ events .= html_entity_decode (wordwrap ($ event ->description , 80 , "<br/> " ));
43
- $ events .="</p><br/> $ event ->venue_name <br/> $ event ->venue_address <br/> " ;
44
- $ events .="$ event ->venue_city , $ event ->venue_state_name " ;
45
- $ events .="<p><a href= $ event ->ticket_url >Buy Tickets</a></p></div> " ;
46
- }
47
- }
48
- // No results were returned
49
- if (emptyempty ($ events )){
50
- $ events = "Sorry, no events matching $ query in $ ___location " ;
24
+ // function calling the opensocial makerequest method
25
+ function runQuery (query , handler ) {
26
+ gadgets . io . makeRequest (BASE_URI , handler , {
27
+ METHOD : ' POST' ,
28
+ POST_DATA : toQueryString ({q : query , format : ' json' }),
29
+ CONTENT_TYPE : ' JSON' ,
30
+ AUTHORIZATION : ' OAuth'
31
+ });
32
+ };
33
+
34
+ // Tool function to create a request string
35
+ function toQueryString (obj ) {
36
+ var parts = [];
37
+ for (var each in obj ) if (obj . hasOwnProperty (each )) {
38
+ parts . push (encodeURIComponent (each ) + ' =' +
39
+ encodeURIComponent (obj [each ]));
51
40
}
52
- // Display results and unset the global array $_GET
53
- echo $ events ;
54
- unset($ _GET );
55
- }
56
- ?>
41
+ return parts . join (' &' );
42
+ };
43
+
44
+ // Run YQL query to GeoPlanet API and extract data from response
45
+ runQuery (' select * from geo.places where text="' + ___location + ' "' ,
46
+ function (rsp ) {
47
+ if (rsp . data ){
48
+ var place = rsp . data . query . results . place ;
49
+ if (place [0 ]){
50
+ placeplace = place [0 ];
51
+ }
52
+ var name = place . name || ' Unknown' ;
53
+ var country = place . country . content || place [0 ]. country . content ||
54
+ ' Unknown' ;
55
+ var latitude = place . centroid . latitude || ' Unknown' ;
56
+ var longitude = place . centroid . longitude || ' Unknown' ;
57
+ var city = place . locality1 . content || ' Unknown' ;
58
+ var state = place . admin1 . content || ' Unknown' ;
59
+ var county = place . admin2 . content || ' Unknown' ;
60
+ var zip = place . postal ? place . postal . content : ' Unknown' ;
61
+
62
+ content = ' <ul><li><strong>Place Name: </strong>' + name + ' </li>' +
63
+ ' <li><strong>City/Town: </strong>' + city + ' </li>' +
64
+ ' <li><strong>County/District: </strong>' + county + ' </li>' +
65
+ ' <li><strong>State/Province: </strong>' + state + ' </li>' +
66
+ ' <li><strong>Zipcode: </strong>' + zip + ' </li>' +
67
+ ' <li><strong>Country: </strong>' + country + ' </li>' +
68
+ ' <li><strong>Latitude: </strong>' + latitude + ' </li>' +
69
+ ' <li><strong>Longitude: </strong>' + longitude + ' </li></ul>' ;
70
+ container . innerHTML = content ;
71
+ }
72
+ else {
73
+ container . innerHTML = gadgets . json . stringify (rsp );
74
+ }
75
+ } );
76
+ }
77
+ // Create an event handler for submitting the form
78
+ var form = document.getElementById('geosearch');
79
+ form.addEventListener('submit',makeQuery,false);
80
+ </script >
0 commit comments