Skip to content

Commit 5c3fe0f

Browse files
committed
commit left of friday
1 parent 58889d7 commit 5c3fe0f

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

yql_test.php

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,56 @@
1-
<?php
2-
$headers = array(
3-
"Content-type: text/xml;charset=\"utf-8\"",
4-
"Accept: text/xml",
5-
"Cache-Control: no-cache",
6-
"Pragma: no-cache",
7-
"SOAPAction: \"run\""
8-
);
9-
$url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20upcoming.events%20where%20woeid%20in%20(select%20woeid%20from%20geo.places%20where%20text%3D%22North%20Beach%22)%20%7C%20sort(field%3D%22start_date%22)%20%7C%20truncate(count%3D1)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
10-
$soap_do = curl_init();
11-
curl_setopt($soap_do, CURLOPT_URL, $url );
12-
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
13-
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
14-
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
15-
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
16-
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
17-
18-
//curl_setopt($soap_do, CURLOPT_POSTFIELDS, $post_string);
19-
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
20-
curl_setopt($soap_do, CURLOPT_USERPWD, '[email protected]' . ":" . "p!(i@n");
21-
22-
$result = curl_exec($soap_do);
23-
$err = curl_error($soap_do);
24-
//var_dump($result);
25-
26-
$dom = new DOMDocument;
27-
$dom->loadXML($result);
28-
29-
30-
?>
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: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name='event' id='event' type='text' size='20'/><br/>
5+
<p><button id='find_event'>Find Event</button></p>
6+
</form>
7+
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";
20+
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";
29+
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";
51+
}
52+
// Display results and unset the global array $_GET
53+
echo $events;
54+
unset($_GET);
55+
}
56+
?>

0 commit comments

Comments
 (0)