Integrate Eventbrite API in PHP
- Before diving into code, ensure you have a registered application with Eventbrite to get your API keys and configure OAuth settings.
- Utilize Composer to manage dependencies and install the HTTP client like Guzzle to interact with the Eventbrite API. Use the following command to install Guzzle:
composer require guzzlehttp/guzzle
Authenticate and Obtain Access Token
- Once Guzzle is installed, use it to handle OAuth authentication. Each request to Eventbrite needs an access token for authentication. You might already have a personal OAuth token that you can use for direct API requests.
- Configure Guzzle with your access token. Here's a PHP example of how to set up your Guzzle client with your token:
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://www.eventbriteapi.com/v3/',
'headers' => [
'Authorization' => 'Bearer YOUR_ACCESS_TOKEN'
]
]);
Fetch Events
- To manage events, you may first want to fetch existing events. You can do this by querying the Eventbrite API's events endpoint:
- Below is an example of making a GET request to fetch events:
$response = $client->request('GET', 'users/me/events');
$events = json_decode($response->getBody(), true);
// Loop through events and print their names
foreach ($events['events'] as $event) {
echo $event['name']['text'] . "<br>";
}
Create an Event
- To add a new event, you need to make a POST request to the events endpoint with appropriate event details:
- Example of creating an event with basic details:
$response = $client->request('POST', 'organizations/YOUR_ORGANIZATION_ID/events/', [
'json' => [
'event' => [
'name' => ['html' => 'Sample Event Name'],
'start' => ['utc' => '2024-05-01T10:00:00Z', 'timezone' => 'UTC'],
'end' => ['utc' => '2024-05-01T18:00:00Z', 'timezone' => 'UTC'],
'currency' => 'USD'
]
]
]);
$newEvent = json_decode($response->getBody(), true);
echo "New event created: " . $newEvent['id'];
Update an Existing Event
- If you need to update an existing event, you'll perform a POST request to the event's specific endpoint using its ID:
- Here's an example of updating an event's name:
$response = $client->request('POST', 'events/EVENT_ID/', [
'json' => [
'event' => [
'name' => ['html' => 'Updated Event Name']
]
]
]);
$updatedEvent = json_decode($response->getBody(), true);
echo "Event updated: " . $updatedEvent['id'];
Delete an Event
- To delete an event, make a DELETE request to the specific event endpoint. Note that deleting events should be used cautiously:
- Example of deleting an event:
$response = $client->request('DELETE', 'events/EVENT_ID/');
echo "Event deleted successfully.";