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) ){ throw new Exception( $resp->error->error_message ); } } 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 = "
"; }else{ $html .= "