Integrating with EventBrite

Update 1/30/2011: I have added a key piece of code to see if any events exist. See the code at the bottom for details

When we launched The David Pettigrew Memorial Foundation's new home in December, there were a few key integration points we needed to hit. First and foremost was integration with EventBrite, which they used to schedule and post their Free Safety Workshops.

We didn't need to display each class on the site, but wanted to post the next class of each type (there were 3) which people could sign up for (weren't sold out). Finally we wanted to break down the date and show the current signups for the class in a visual way.

First we took a nice Event Brite PHP class
class eventbrite
{
  const http_interface = 'auto'; //'auto': autodetect, 'curl' or 'fopen'

  private $request_url = 'https://www.eventbrite.com/xml/';
  private $app_key = '';
  private $user_key = '';
  private $org_id = '';
  private $creds;
  private $results = array();

  /**
   * Constructor function for all new gapi instances
   *
   * Set up authenticate with EventBrite and get auth_token
   *
   * @param String $email
   * @param String $password
   * @param String $token
   * @return gapi
   */
  public function __construct()
  {
        #$this->app_key = $app_key;
        #$this->user_key = $user_key;
        #$this->org_id = $org_id;
        $this->creds = "&app_key=".$this->app_key."&user_key=".$this->user_key."&id=".$this->org_id;
  }

  private function curl($url)
  {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $re = curl_exec($ch);
        curl_close($ch);
        return $re;
  }

public function list_events()
  {
        $data = array();
        $url = $this->request_url."organizer_list_events?".$this->creds;
        $hd = $this->curl($url);
        $xml = simplexml_load_string($hd);
        return $xml;
  } 

public function srch_events($keys, $org = null, $date = null, $max = null)
  {
        $url = $this->request_url."event_search?".$this->creds."&sort_by=date&keywords=".urlencode($keys);
        $url .= $date ? "&date=".$date : "";
        $url .= $org ? "&organizer=".$org : "";
        $url .= $max ? "&max=".$max : "";
        $hd = $this->curl($url);
        $xml = simplexml_load_string($hd);
        return $xml;
  }
}
Now we knew the ogranizer, and we knew the name of each different type of class, so we ran a search for each class type. Note that the EventBrite API allows for keywords as date ranges (such as future or past, so that saves us from having to calculate and send today's date. Also, since we are only displaying one event, and the events are returned in the order of the next occurring is first, we can limit the result to 1 to save on bandwidth of the search and speed up the process.
 $eb = new eventbrite;

 // Event Search: @keywords, @organizer, @date, @max
 $ava = $eb->srch_events("Mountain Safety Workshop", "Pettigrew", "Future", "1");

 $spots_left = $ava->event->tickets->ticket->quantity_available-$ava->event->tickets->ticket->quantity_sold; 

// Display
echo ">r;a href="$ava->event->url" target="_blank">$ava->event->title
";
echo "$ava->event->venue->name";
echo "
"; // Display a "filled" icon for each ticket sold for($i = 1; $i <= $ava->event->tickets->ticket->quantity_sold; $i++) { echo ">r;img src="images/filled.png" alt="" /> "; } // Then display an "open" icon for each ticket remaining for($i = 1; $i <= $spots_left; $i++) { echo ">r;img src="images/open.png" alt="" /> "; } echo "
";
I left out the date formatting, all it involves is using event->start_date and formatting it the way you'd like, then breaking it down.
// Check if any results were returned from the request
$srch = $eb->srch_events("Mountain Safety Workshop", "Pettigrew", "Future", "1");
$no_results = strpos($srch->error_message, "No events found");
/* Important: Use the triple = here! The match will be at the 
start of string and will return 0, therefore if you don't use 
the triple equals it will be converted from 0 to false */
if($no_results === true)
{
     // Put your normal actions here
}else{
    // Put what to do if there is nothing to show here
}
Here's the final result. Without any admin interaction, the events are kept fresh and relevant to the users based on the event capacity and date.
Small dog web development studio
View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News