Welcome back!
It’s time to continue exploring the Eventbrite APIs. Today we’ll write a very simple PHP page finds events very close to a specific latitude and longitude using the event_search API.
What You Need
- Apache, or some similar HTTP server, running PHP 5 or higher
- An Eventbrite app key
Something to Start From
We are not here to learn about writing forms in PHP, so to get us started here is the skeleton of an application to which we will add Eventbrite functionality.
<?php
$app_key = "exampleApiKey"; //Supply your Eventbrite App Key here. Remember to keep your App Key secure!
if (!(isset($_GET['latitude']) && isset($_GET['longitude']))) {
//No coordinates are set. Display a very simple form to request a latitude and longitude
?>
<?php
} else { //We have coordinates! Lets get busy.
//Normally we'd validate the input here, but we'll skip that ugly stuff
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
print "I will search for events near lattitude $latitude longitude $longitude.\n"; }
Go ahead. Paste it into your editor and see how it works. On first load it will display a form with two fields: one for latitude and one for longitude. When submitted the page will print the supplied latitude and longitude values.
Constructing the Query
To fetch a list of events we must query the event_search API. All we need to do is to put together a URL that specifies our event search filters. For this tutorial we need to specify three parameters. Two of these come from our form: latitude and longitude. The third, within distance, we will hard code for the sake of this example. 1 mile seems like a reasonable within distance.
$request_url = "https://www.eventbrite.com/xml/event_search/?app_key=$app_key&latitude=$latitude&longitude=$longitude&within=1";
Executing the Request and Interpreting the Response
We can now use our favorite PHP HTTP client, cURL, to execute our freshly created API request.
After we execute the request we can parse and interpret the response.
If the API request failed, we expect to see an <error> element at the root of the document. This element will contain an <error_message> element. We will display an error message if it is available.
Under normal conditions our API request will not contain an <error> element. In this case the response contains an <events> element with a single <summary> element and many <event> elements as children. We will display the total number of discovered events and list the titles of each event returned. Note that this API uses paging, so for popular areas not all event titles will be printed.
//Next, lets hit the API with cURL
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($ch);
//We have our result in a string. Lets parse it.
$xml = new SimpleXMLElement($response_body);
// var_dump($xml); //Uncomment this line to see the entire parsed response
//Check our parsed response for problems (such as an errors element). If there's an issue, let our user know.
if ($xml->error_message) {
//something is up here
$error_message = $xml->error_message;
print "Eventbrite responded with an error: $error_message
\n";
} else {
//Things are looking good. We can start by printing some of the response summary data
print "I found " . $xml->summary->total_items . " nearby events. Here are the titles from the first page of results: \n";
//The bulk of our response is a list of events. Let's display each event title.
print "\n";
foreach ($xml->event as $event) {
print "- " . $event->title . "
\n";
}
print "
\n";
}
//some boilerplate clean-up stuff
curl_close($ch);
The Result
Visit this PHP page and supply the latitude and longitude for a location that is likely to have some upcoming events. Try a latitude of 37.78 and a longitude of -122.4. This will search a populated part of San Francisco.
You should see something like this:
Hitting
https://www.eventbrite.com/xml/event_search/?app_key=APP_KEY&latitude=37.78&longitude=-122.4&within=1 to find nearby events.I found 369 nearby events. Here are the titles from the first page of results:
- Manor West
- Signature Series at MANOR WEST DJ Z-TRIP
- “Signature Series” at Manor West ~ featuring Yolanda Be Cool: “We No Speak Americano”
- Elite Session With STIMMING
- FIP Seminar at San Francisco WestEd
- Bridal Stylist Training & Certification
- Image Consultant Certification Training
- Learn Color Analysis Get Certified Too!
- MAKEOVER YOU and Get the Date, Job, Money!!
- City Nights Mardi Gras 2011 featuring DJ CLASS
Knock It Up a Notch
We have a list of events within a mile of our latitude and longitude. If we happen to be carrying our GPS and are simply curious about what is going on around us, this may be useful, but there is so much more that we can do with this API. Here are a few ideas for features that you may wish to add to our search tool:
- Plot the nearby events on a Google Map.
- Use HTML5 geolocation to remove the need for that pesky latitude and longitude form.
All of the Code
Did some of my instructions confuse you? Would you like to skip all my wordy explanation and just hack at the code? If so, here is the complete code for this tutorial.
Enjoy!
<?php
$app_key = "exampleApiKey"; //Supply your Eventbrite App Key here. Remember to keep your App Key secure!
if (!(isset($_GET['latitude']) && isset($_GET['longitude']))) {
//No coordinates are set. Display a very simple form to request a latitude and longitude
?>
<?php
} else { //We have coordinates! Lets get busy.
//Normally we'd validate the input here, but we'll skip that ugly stuff
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
print "I will search for events near lattitude $latitude longitude $longitude.\n"; //First, we need to construct our request URL $request_url = "https://www.eventbrite.com/xml/event_search/?app_key=$app_key&latitude=$latitude&longitude=$longitude&within=1"; print "Hitting
$request_url to find nearby events.\n”; //Next, lets hit the API with cURL $ch = curl_init($request_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response_body = curl_exec($ch); //We have our result in a string. Lets parse it. $xml = new SimpleXMLElement($response_body); // var_dump($xml); //Uncomment this line to see the entire parsed response //Check our parsed response for problems (such as an errors element). If there’s an issue, let our user know. if ($xml->error_message) { //something is up here $error_message = $xml->error_message; print “Eventbrite responded with an error: $error_message
\n”; } else { //Things are looking good. We can start by printing some of the response summary data print “I found ” . $xml->summary->total_items . ” nearby events. Here are the titles from the first page of results: \n”; //The bulk of our response is a list of events. Let’s display each event title. print “
- \n”;
foreach ($xml->event as $event) {
print “
- ” . $event->title . “ \n”; } print “