Protect API Methods
URL : https://github.com/stas/php-eventbrite/blob/master/EBAPI.class.php
protected $api_methods = array(
Discount
‘discount_new’ => array( ‘event_id’, ‘code’, ‘amount_off’, ‘percent_off’, ‘tickets’, ‘quantity_available’, ‘start_date’, ‘end_date’ ),
‘discount_update’ => array( ‘id’, ‘code’, ‘amount_off’, ‘percent_off’, ‘tickets’, ‘quantity_available’, ‘start_date’, ‘end_date’ ),
Events
‘event_copy’ => array( ‘event_id’, ‘event_name’ ),
‘event_get’ => array( ‘id’ ),
‘event_list_attendees’ => array( ‘id’, ‘count’, ‘page’, ‘do_not_display’, ‘show_full_barcodes’ ),
‘event_list_discounts’ => array( ‘id’ ),
‘event_new’ => array( ‘title’, ‘description’, ‘start_date’, ‘end_date’, ‘timezone’, ‘privacy’, ‘personalized_url’, ‘venue_id’, ‘organizer_id’, ‘capacity’, ‘currency’, ‘status’, ‘custom_header’, ‘custom_footer’, ‘background_color’, ‘text_color’, ‘link_color’, ‘title_text_color’, ‘box_background_color’, ‘box_text_color’, ‘box_border_color’, ‘box_header_background_color’, ‘box_header_text_color’ ),
‘event_search’ => array( ‘keywords’, ‘category’, ‘address’, ‘city’, ‘region’, ‘postal_code’, ‘country’, ‘within’, ‘within_unit’, ‘latitude’, ‘longitude’, ‘date’, ‘date_created’, ‘date_modified’, ‘organizer’, ‘max’, ‘count_only’, ‘sort_by’, ‘page’, ‘since_id’, ‘tracking_link’ ),
‘event_update’ => array( ‘event_id’, ‘title’, ‘description’, ‘start_date’, ‘end_date’, ‘timezone’, ‘privacy’, ‘personalized_url’, ‘venue_id’, ‘organizer_id’, ‘capacity’, ‘currency’, ‘status’, ‘custom_header’, ‘custom_footer’, ‘background_color’, ‘text_color’, ‘link_color’, ‘title_text_color’, ‘box_background_color’, ‘box_text_color’, ‘box_border_color’, ‘box_header_background_color’, ‘box_header_text_color’ ),
Organizer
‘organizer_list_events’ => array( ‘id’ ),
‘organizer_new’ => array( ‘name’, ‘description’ ),
‘organizer_update’ => array( ‘id’, ‘name’, ‘description’ ),
Payment
‘payment_update’ => array( ‘event_id’, ‘accept_paypal’, ‘paypal_email’, ‘accept_google’, ‘google_merchant_id’, ‘google_merchant_key’, ‘accept_check’, ‘instructions_check’, ‘accept_cash’, ‘instructions_cash’, ‘accept_invoice’, ‘instructions_invoice’ ),
Ticket
‘ticket_new’ => array( ‘event_id’, ‘is_donation’, ‘name’, ‘description’, ‘price’, ‘quantity’, ‘start_sales’, ‘end_sales’, ‘include_fee’, ‘min’, ‘max’ ),
‘ticket_update’ => array( ‘id’, ‘is_donation’, ‘name’, ‘description’, ‘price’, ‘quantity’, ‘start_sales’, ‘end_sales’, ‘include_fee’, ‘min’, ‘max’, ‘hide’ ),
User
‘user_get’ => array( ‘user_id’, ’email’ ),
‘user_list_events’ => array( ‘user’, ‘do_not_display’, ‘event_statuses’, ‘asc_or_desc’ ),
‘user_list_organizers’ => array( ‘user’, ‘password’ ),
‘user_list_tickets’ => array(),
‘user_list_venues’ => array( ‘user’, ‘password’ ),
‘user_new’ => array( ’email’, ‘password’ ),
‘user_update’ => array( ‘new_email’, ‘new_password’ ),
Venue
‘venue_new’ => array( ‘organizer_id’, ‘venue’, ‘adress’, ‘adress_2’, ‘city’, ‘region’, ‘postal_code’, ‘country_code’ ),
‘venue_update’ => array( ‘id’, ‘venue’, ‘adress’, ‘adress_2’, ‘city’, ‘region’, ‘postal_code’, ‘country_code’ )
);
App Key link:
http://www.eventbrite.com/myaccount/apps/
User Key:
https://www.eventbrite.com/userkeyapi
Sample File :
<?php
/**
* File Name : Test.php
* Created by : Amit Sabal
* Created on : 25th July 2015
* Motive : To test the EventBrite API
*/
ini_set('display_errors',0);
/**
* load the API Client library
*/
include "../Eventbrite.php";
/**
* Authorization tokens are outlined here:
* http://developer.eventbrite.com/doc/authorization/
*
* Creating object of the EventBrite Class
*/
$eb = new Eventbrite(
array(
'app_key'=>'EAMRSGJTCHNUMRIEEB',
'user_key'=>'1437540524147554554645'
)
);
try{
/**
* Docs on how to use the API's event_new method are located here:
* http://developer.eventbrite.com/doc/events/event_new/
*/
// To create venue of the event
$venueDetails = $eb->venue_new(
array(
'organizer_id' => '8331193967',
'venue' => 'Gopalan Mall',
'adress' => '3rd Floor',
'adress_2' => '',
'city' => 'Bangaluru',
'region' => 'Karnataka',
'postal_code' => '560018',
'country_code' => 'IN',
)
);
// Fetching the Venue ID
$venue_id = $venueDetails->process->id;
// To create the Event
$createEvent = $eb->event_new(
array(
'title' => 'Test Amit Event 3',
'start_date' => '2015-07-28 14:00:00',
'end_date' => '2015-07-30 22:00:00',
'status' => 'Live',
'shareable' => '1',
'privacy' => '1',
'organizer_id' => '8331193967',
'venue_id' => $venue_id,
//'custom_header' => '<img src="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg" width="100%"/>'
)
);
// Fetching the newly created Event ID
$event_id = $createEvent->process->id;
// Update the existing Event based on the ID
$eventUpdate = $eb->event_update(
array(
'event_id' => '17858523324',
'title' => 'First Test Event',
)
);
// To create the Ticket
$ticketCreate = $eb->ticket_new(
array(
'event_id' => $event_id,
'is_donation' => '0',
'name' => 'Confer',
'description' => 'this is the test ticket',
'price' => '0',
'quantity' => '10',
'start_sales' => '2015-07-01 14:00:00',
'end_sales' => '2015-07-30 22:00:00',
'include_fee' => '',
'min' => '',
'max' => '',
'status' => '1',
)
);
// Fetching the Ticket ID
$ticket_id = $ticketCreate->process->id;
// Update the existing Ticket based on the Ticket ID
// Still to test
$ticketUpdate = $eb->ticket_update(
array(
'id' => '37823530',
'is_donation' => '1',
'name' => 'Global Meet',
'include_fee' => '',
'min' => '',
'max' => '',
'status' => '1',
'hide' => '1',
)
);
// Searching the Event based on the parameter passed.
/**
$eventSearch = $eb->event_search(
array(
'keywords' => '',
'category' => '',
'address' => '',
'city' => '',
'region' => '',
'postal_code' => '560085',
'country' => '',
'within' => '',
'within_unit' => '',
'latitude' => '',
'longitude' => '',
'date' => '',
'date_created' => '',
'date_modified' => '',
'organizer' => '',
'max' => '',
'count_only' => '',
'sort_by' => '',
'page' => '',
'since_id' => '',
'tracking_link' => '',
)
);
*/
// Still to test
$eventSearch = $eb->event_search(
array(
'keywords' => 'First',
'category' => '',
'address' => '',
'city' => '',
'region' => '',
'postal_code' => '',
'country' => '',
'within' => '',
'within_unit' => '',
'latitude' => '',
'longitude' => '',
'date' => '',
'date_created' => '',
'date_modified' => '',
'organizer' => '',
'max' => '',
'count_only' => '',
'sort_by' => '',
'page' => '',
'since_id' => '',
'tracking_link' => '',
)
);
// To list the user events
$events = $eb->user_list_events();
}catch( Exception $e){
var_dump($e);
}
?>
<h1>List User Events:</h1>
<?= Eventbrite::eventList( $events, 'eventListRow'); ?>
eventbrite.php
<?php
class Eventbrite {
/**
* Eventbrite API endpoint
*/
var $api_endpoint = “https://www.eventbrite.com/json/”;
var $auth_tokens;
var $api_url;
/**
* Eventbrite API key (REQUIRED)
* http://www.eventbrite.com/api/key/
* Eventbrite user_key (OPTIONAL, only needed for reading/writing private user data)
* http://www.eventbrite.com/userkeyapi
*
* Alternate authorization parameters (instead of user_key):
* Eventbrite user email
* Eventbrite user password
*/
function __construct( $tokens = null, $user = null, $password = null ) {
$this->api_url = parse_url($this->api_endpoint);
$this->auth_tokens = array();
if(is_array($tokens)){
if(array_key_exists(‘access_code’, $tokens)){
$this->auth_tokens = $this->oauth_handshake( $tokens );
}else{
$this->auth_tokens = $tokens;
}
}else{
$this->auth_tokens[‘app_key’] = $tokens;
if( $password ){
$this->auth_tokens[‘user’] = $user;
$this->auth_tokens[‘password’] = $password;
}
else {
$this->auth_tokens[‘user_key’] = $user;
}
}
}
function oauth_handshake( $tokens ){
$params = array(
‘grant_type’=>’authorization_code’,
‘client_id’=> $tokens[‘app_key’],
‘client_secret’=> $tokens[‘client_secret’],
‘code’=> $tokens[‘access_code’] );
$request_url = $this->api_url[‘scheme’] . “://” . $this->api_url[‘host’] . ‘/oauth/token’;
// TODO: Replace the cURL code with something a bit more modern –
//$context = stream_context_create(array(‘http’ => array(
// ‘method’ => ‘POST’,
// ‘header’ => “Content-type: application/x-www-form-urlencoded\r\n”,
// ‘content’ => http_build_query($params))));
//$json_data = file_get_contents( $request_url, false, $context );
// CURL-POST implementation –
// WARNING: This code may require you to install the php5-curl package
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
$response = get_object_vars(json_decode($json_data));
if( !array_key_exists(‘access_token’, $response) || array_key_exists(‘error’, $response) ){
throw new Exception( $response[‘error_description’] );
}
return array_merge($tokens, $response);
}
// For information about available API methods, see: http://developer.eventbrite.com/doc/
function __call( $method, $args ) {
// Unpack our arguments
if( is_array( $args ) && array_key_exists( 0, $args ) && is_array( $args[0]) ){
$params = $args[0];
}else{
$params = array();
}
// Add authentication tokens to querystring
if(!isset($this->auth_tokens[‘access_token’])){
$params = array_merge($params, $this->auth_tokens);
}
// Build our request url, urlencode querystring params
$request_url = $this->api_url[‘scheme’].”://”.$this->api_url[‘host’].$this->api_url[‘path’].$method.’?’.http_build_query( $params,”,’&’);
// Call the API
if(!isset($this->auth_tokens[‘access_token’])){
$resp = file_get_contents( $request_url );
}else{
$options = array(
‘http’=>array( ‘method’=> ‘GET’,
‘header’=> “Authorization: Bearer ” . $this->auth_tokens[‘access_token’])
);
$resp = file_get_contents( $request_url, false, stream_context_create($options));
}
// parse our response
if($resp){
$resp = json_decode( $resp );
if( isset( $resp->error ) && isset($resp->error->error_message) ){
//echo $resp->error->error_message;
// throw new Exception( $resp->error->error_message );// Commented as it is giving error
}
}
return $resp;
}
/*
* Helpers:
*/
public static function OAuthLogin($auth_tokens, $get_token=’getAccessToken’, $save_token=’saveAccessToken’, $delete_token=’deleteAccessToken’){
$user = false;
$response = array();
# Attempt to authenticate this user using an access_token, if available
if(!isset($auth_tokens[‘access_token’])){
if(is_callable($get_token)){
$auth_tokens[‘access_token’] = $get_token();
}elseif(is_callable(array(‘self’,$get_token))){
$auth_tokens[‘access_token’] = self::$get_token();
}
}
if( isset($auth_tokens[‘access_token’]) ){
try{
// Example using an access_token to initialize the API client:
$eb = new Eventbrite(array(‘access_token’ => $auth_tokens[‘access_token’]));
$user = $eb->user_get()->user;
}catch(Exception $e){
$user = false;
// This token may no longer be valid
// refresh it, or clear it
$response[‘login_error’] = $e->getMessage();
if(is_callable($delete_token)){
$delete_token( $auth_tokens[‘access_token’] );
}elseif(is_callable(array(‘self’,$delete_token))){
self::$delete_token( $auth_tokens[‘access_token’] );
}
}
}
# We do not have a valid access token for this user so far
if( $user == false ){
# This user is not yet authenticated –
# it is their first visit,
# or they are returning with an access_code that we will exchange for an access_token,
# or they were redirected here after logout
if( isset($auth_tokens[‘access_code’]) ){
# This user has just authenticated, get their access token and store it
try{
$eb = new Eventbrite($auth_tokens );
$response[‘access_token’] = $eb->auth_tokens[‘access_token’];
// save this access_token for future use!
if(is_callable($save_token)){
$save_token( $response[‘access_token’] );
}elseif(is_callable(array(‘self’,$save_token))){
self::$save_token( $response[‘access_token’] );
}
header(‘Location: ‘ . $_SERVER[‘PHP_SELF’] );
exit;
}catch (Exception $e){
$response[‘login_error’] = $e->getMessage();
}
}else if( isset($auth_tokens[‘error_message’] )){
if($auth_tokens[‘error_message’] == ‘access_denied’){
$response[‘login_error’] = “Account access denied.”;
}else{
$response[‘login_error’] = $auth_tokens[‘error_message’];
}
}
}else if(is_object($user)){
$response[‘user_email’] = $user->email;
$response[‘user_name’] = $user->first_name . ‘ ‘ . $user->last_name;
}
return $response;
}
public static function widgetHTML( $params ){
// Replace this example with something that works with your Application’s templating engine
$html = ”
Eventbrite Account Access
“;
if( isset($params[‘user_name’]) && isset($params[‘user_email’]) && isset($params[‘logout_link’]) ){
$html .= ”
Welcome Back!
“;
$html .= ”
You are logged in as:
{$params[‘user_name’]}
({$params[‘user_email’]})
“;
$html .= ”
Logout
“;
}elseif( isset($params[‘oauth_link’]) ){
if(isset($params[‘login_error’])){
$html .= “<p class=’error’>{$params[‘login_error’]}</p>”;
}
$html .= “<p><a class=’button’ href='{$params[‘oauth_link’]}’>Login with Eventbrite</a></p></div>”;
}else{
$html .= ”
Eventbrite widgetHTML template example fail 😦
“;
}
$html .= “</div>”;
return $html;
}
public static function oauthNextStep( $key ) {
return ‘https://www.eventbrite.com/oauth/authorize?response_type=code&client_id=’.$key;
}
public static function eventList($evnts= array(), $callback=’eventListRow’, $options=false) {
$html=’
‘;
if( isset($evnts->events)){
foreach( $evnts->events as $evnt ){
if( isset($evnt->event ) ){
if(is_callable($callback)){
if($options){
$html .= $callback($evnt->event, $options);
}else{
$html .= $callback($evnt->event);
}
}else if(is_callable( array(‘self’, $callback))){
if($options){
$html .= self::$callback($evnt->event, $options);
}else{
$html .= self::$callback($evnt->event);
}
}
}
}
}else{
$html .= “No events were found at this time.”;
}
return $html . “
“;
}
public static function getAccessToken( ) {
if(isset($_SESSION[‘EB_OAUTH_ACCESS_TOKEN’])){
return $_SESSION[‘EB_OAUTH_ACCESS_TOKEN’];
}else{
return null;
}
}
public static function saveAccessToken( $access_token ) {
// this function should save the existing user’s access_token.
$_SESSION[‘EB_OAUTH_ACCESS_TOKEN’] = $access_token;
}
public static function deleteAccessToken( ) {
// this function should remove the existing user’s access_token.
unset($_SESSION[‘EB_OAUTH_ACCESS_TOKEN’]);
}
public static function eventListRow( $evnt ) {
$time = strtotime($evnt->start_date);
$venue_name = ‘online’;
if( isset($evnt->venue) && isset( $evnt->venue->name )){
$venue_name = $evnt->venue->name;
}
return ”
id .”‘>
” . strftime(‘%a, %B %e’, $time) . “” . strftime(‘%l:%M %P’, $time) . “” .”
url.”‘>”.$evnt->title.”” . $venue_name . “
\n”;
}
/*
* Widgets:
*/
public static function loginWidget( $options, $get_token=’getAccessToken’, $save_token=’saveAccessToken’, $delete_token=’deleteAccessToken’, $render_login_box=’widgetHTML’ ){
if( ( isset($options[‘logout_link’])
&& $options[‘logout_link’] == $_SERVER[‘REQUEST_URI’] )
// TODO: add a way to disable this default:
|| ( isset($_GET[‘eb_logout’])
&& $_GET[‘eb_logout’]==”true” )) {
// clear this user’s access_token –
Eventbrite::deleteAccessToken();
// remove our “logout=true” trigger from the querystring-
header(“Location: ” . $_SERVER[‘PHP_SELF’]);
exit;
}
// automatically pull the access_code from the querysting?
// TODO: add a way to disable this:
if(!isset($options[‘access_code’] )){
$options[‘access_code’] = isset($_REQUEST[‘code’]) ? $_REQUEST[‘code’] : null;
}
// automatically grab errors from the querystring?
// TODO: add a way to disable this:
if(!isset($options[‘error_message’])){
$options[‘error_message’] = isset($_REQUEST[‘error’]) ? $_REQUEST[‘error’] : null;
}
// Check to see if we have a valid user account
// and Proccess any data-related work:
$response = Eventbrite::OAuthLogin($options, $get_token, $save_token, $delete_token);
// package up the data for our view / template:
$login_params = array();
if( is_array($response)){
if( isset( $response[‘user_email’]) ){
$login_params = array(‘user_name’ => $response[‘user_name’],
‘user_email’ => $response[‘user_email’]);
}
$login_params[‘oauth_link’] = Eventbrite::oauthNextStep($options[‘app_key’]);
if(isset( $response[‘login_error’])){
$login_params[‘login_error’] = $response[‘login_error’];
}
if(isset( $options[‘logout_link’])){
$login_params[‘logout_link’] = $options[‘logout_link’];
}else{
$login_params[‘logout_link’] = $_SERVER[‘PHP_SELF’] . ‘?eb_logout=true’;
}
}
// view related work:
// render your “template”
if(is_callable($render_login_box)){
return $render_login_box( $login_params );
}elseif(is_callable(array(‘self’,$render_login_box))){
return self::$render_login_box( $login_params );
}else{
//the templating callback was not valid,
//return the raw data for use with an external template
return $login_params;
}
}
public static function ticketWidget( $evnt, $height=’300px’, $width=’100%’ ) {
return ‘
‘;
}
public static function registrationWidget( $evnt ) {
return ‘
‘;
}
public static function calendarWidget( $evnt ) {
return ‘
‘;
}
public static function countdownWidget( $evnt ) {
return ‘
‘;
}
public static function buttonWidget( $evnt ) {
return ‘<a href=”http://www.eventbrite.com/event/’ . $evnt->id . ‘?ref=ebtn” target=”_blank”><img border=”0″ src=”http://www.eventbrite.com/custombutton?eid=’ . $evnt->id . ‘” alt=”Register for ‘ . $evnt->title . ‘ on Eventbrite” /></a>’;
}
public static function linkWidget( $evnt, $text=null, $color=null ) {
return ‘<a href=”http://www.eventbrite.com/event/’ . $evnt->id . ‘?ref=elink” target=”_blank” style=”color:’ . ( $color ? $color : “#000000″ ) . ‘;”>’ . ( $text ? $text : $evnt->title ) . ‘</a>’;
}
};
?>