Jump to content

Eventbrite v3 API & ProcessWire


psy
 Share

Recommended Posts

Hi, me again - queen of the ProcessWire API Wrapper. Why re-invent the wheel when there are so many great, well-supported apps out there that don't impact on the core of PW? Almost see PW as a portal.

Latest endeavour is a PW module for Eventbrite v3 API https://www.eventbrite.com/developer/v3/

The EB webhooks are a easy-peasy-lemon-squeezy. Love it!

Can do any of the API GET calls providing they don't include any parameters, eg:

$eb = $modules('EventbriteAPI')->login();

$ebUser = $eb->client->get('/users/me/');
$ebUserId = $ebUser['id'];

// /users/:id/owned_events/
$result = $eb->get("/users/$ebUserId/owned_events");

Problem I'm having is with using parameters in the actual API, eg:

$eb = $modules('EventbriteAPI')->login();

$ebUser = $eb->client->get('/users/me/');
$ebUserId = $ebUser['id'];
$ebPath = "/users/$ebUserId";

$token = $sanitizer->text($eb->access_code);
$pageSize = $sanitizer->int(5);
$expand = array();

$result = $eb->client->get( $ebPath . "/events?&token=" . $eb->access_code . "&page_size=" . $pageSize );

Every time I use a parameter, I get the EB error message for the last one, in the example below, the paging limit:

"There are errors with your arguments: page_size - Enter a whole number."

Even stranger is if I manually create the URL, eg 

https://www.eventbriteapi.com/v3/users/2425020999999/events?&token=WNU7SPIEUUUIXXXXXX&page_size=5

in the EB API test environment, I get the correct result.

Fairly certain it's an EB issue but wanting to double check I've got everything right and the PW API isn't doing anything too clever with the $_GET vars.

Any help/suggestions most welcome.

Thanks

psy

 

  • Like 3
Link to comment
Share on other sites

It was a guess, since the error points to a whole number but works when you manually enter the data.

So the echo'd $result is identical to your manual entry?

This is in the test environment only, correct?

Link to comment
Share on other sites

it's not just the 'whole number' error. If I remove the page_size var, I get a similar error on the token var and I know $eb->access_code outputs the correct value. Seems like EB doesn't recognise the end of the URL when submitted via the API.

Just want to confirm it's not me doing something dumb in the module code

Link to comment
Share on other sites

If the url (with manual query params) works when pasted into the address bar, but the exact same url (with query params) does not work when invoked through code, then I would next compare the headers.

Also, have you tried url encoding/building the query string? I noticed the following in your example code:

"/events?&token="
               ^

 

Link to comment
Share on other sites

Thanks @rick, feel like I'm channelling Thomas Edison - "I have not failed. I've just found 10000 ways that won't work."  Tried everything combination I can think of and now very tired. Have posted in the Google EB support group but not hopeful of an answer.

Maybe with a clear head tomorrow...

Appreciate your suggestions :) 

  • Like 1
Link to comment
Share on other sites

@rick overcame that hurdle, now onto next one.

Eventbrite is a great app but unfortunately :

  • their v3 API doco is at best minimal and often misleading/confusing,
  • the errors it reports are correct but don't accurately describe the exact problem, and
  • what works on their browser based debug page doesn't always translate to the actual PHP API.

In the above case, the order of the $_GET vars is important and when using one $param, the doco doesn't mention you also require another.  In my case above, I needed both page_size and page (page_number). All good now.

Still working on getting it all into a functioning PW module. :) 

  • Like 3
Link to comment
Share on other sites

Getting closer to an initial release. Def been a learning experience using EB's self-proclaimed "easy API".

Tip for other devs when implementing other vendors' API's - check the github pull requests before committing hari-kari. In my case https://github.com/eventbrite/eventbrite-sdk-php/issues/7 - wasn't me after all, just a MAJOR bug in the API. My alpha version has the suggested solution implemented and all good so far

What I can say is that if you only want to pull info from your own EB stuff into PW, you won't need my module. Simply set up the necessary in EB and create a PW page/template to handle the web hooks, eg when an event is created in Eventbrite, you can pull that info into PW with:

// Retrieve the request's body and parse it as JSON
$eventbrite_json = @file_get_contents("php://input");
$eventbrite_obj = json_decode($eventbrite_json);

/**
 * Eventbrite webhooks
 * attendee.checked_in - Triggered when an attendee’s barcode is scanned in.
 * attendee.checked_out - Triggered when an attendee’s barcode is scanned out.
 * attendee.updated - Triggered when attendee data is updated.
 * event.created - Triggered when an event is initially created.
 * event.published - Triggered when an event is published and made live.
 * event.updated - Triggered when event data is updated.
 * event.unpublished - Triggered when an event is unpublished.
 * order.placed - Triggers when an order is placed for an event. Generated Webhook’s API endpoint is to the Order endpoint.
 * order.refunded - Triggers when an order is refunded for an event.
 * order.updated - Triggers when order data is updated for an event.
 * organizer.updated - Triggers when organizer data is updated.
 * ticket_class.created - Triggers when a ticket class is created.
 * ticket_class.deleted - Triggers when a ticket class is deleted.
 * ticket_class.updated - Triggers when a ticket class is updated.
 * venue.updated - Triggers when venue data is updated.
 */
switch ($eventbrite_obj->config->action) {
    case 'event.created':
        $apiUrl = $eventbrite_obj->api_url;
        $apiUrlArr = explode('/events/', $apiUrl);
        $eventId = str_ireplace('/', '', $apiUrlArr[1]);

        $template = wire('templates')->get('name=test-eventbrite-webhook');
        $p = new Page();
        $p->template = $template;
        $p->title = $sanitizer->text($eventId);
        $p->name = $sanitizer->pageName($eventId);
        $p->parent = pages(1);
        $p->save();
        break;

    default:
        break;

}

// tell eventbrite the webhook was received.
http_response_code(200); // PHP 5.4 or greater

 

  • Like 4
Link to comment
Share on other sites

Channelling Thomas Edison again. Problem I've having now is not Eventbrite but PW.

The module is configured to not autoload although I've tried that too.

I have 2 page templates:

1. Calls my EventbriteAPIv3 module that returns data to a 'normal', viewable page template. All good there

2. Calls my EventbriteAPIv3 in the EB web hook endpoint page template. Not matter how I try to configure it, the module is never loaded.

Instead I get any number of PW error messages, eg:

When I use $eb = $modules->get(EventbriteAPIv3)->login() or $eb = wire('modules')->get('EventbriteAPIv3')->login() in the page template:

Error: Exception: Unknown Selector operator: '' -- was your selector value properly escaped? (in /home/xxxx/xxxx.com/wire/core/Selectors.php line 378)

Other error message include, depending on the method I chose to load it:

Error: Uncaught Error: Call to a member function getAccessCode() on null in /home/xxxxxx/xxxxxx.com/site/templates/eventbrite-webhook.php:21 

 

Have even tried putting the $modules->get call it in _init.php without success.

Reading the PW doco, google searching and running out of ways to vary the call yielded nothing. :(

All suggestions, help & guidance most welcome.

  • Like 1
Link to comment
Share on other sites

Developing this module has definitely been a trial by fire. Getting there though and for sure, reading the PW API docs helped and not just what I thought I needed to know but other stuff as well. Was so focussed on:

$eb = $modules->get('EventbriteAPIv3')->login();

in my webhook template which failed every time, I completely overlooked:

$eb = wire('modules')->getModule('EventbriteAPIv3', array('noPermissionCheck' => true))->login();

Of course the webhook template/response is not a logged-in admin with permission to change stuff... Doh!

  • Like 1
Link to comment
Share on other sites

Little bit chuffed with this new function. You can adapt it to create thumbnail images of external web pages.

In my case, it creates a thumbnail image of the publicly viewable Eventbrite event page and attaches it to the PW event page. The $ebResponse is the json_decoded data returned from the webhook call to Eventbrite.

    /**
     * Calls the pagepeeker.com API to generate a thumbnail of the Eventbrite public page
     * @param $ebResponse- the json decoded response from Eventbrite
     * @param $options = refer pagepeeker API doc -
     *     $options['entrypoint'] - Free 'free'. That makes it: http://free.pagepeeker.com/
     *                             Unbranded 'api'. That makes it: http://api.pagepeeker.com/
     *                             All paid accounts 'api'. That makes it: http://api.pagepeeker.com/
     *
     *    $options['size'] - The size of the thumbnail. Available sizes:
     *                       't'	Tiny, 90 x 68 pixels
     *                       's'	Small, 120 x 90 pixels
     *                       'm'	Medium, 200 x 150 pixels
     *                       'l'	Large, 400 x 300 pixels
     *                       'x'	Extra large, 480 x 360 pixels
     *
     *    $options['code'] - Your API key. This is optional and recommended to use only for server side calls.
     *                       Client side usage does not require it, as we automatically show the thumbnails only
     *                       for the domains added in your account.
     *                       For the free branded accounts, this parameter is ignored, as we provide the free branded
     *                       service for anybody, free of charge.
     *
     *                       Warning
     *                       Never use this code on client side pages. We use it for counting your API calls, so having
     *                       it used by third parties will count against your monthly quota.
     *     $option['template'] - your PW page template
     *     $option ['field']   - your PW page template image field
     *     $option['page']     - the page to retrieve and attach the image to
     *
     * @return mixed
     * @throws WireException
     */

    public function ___pagePeeker ($ebResponse, $options) {
        $entrypoints = array('free', 'api');
        $options['entrypoint'] = in_array($options['entrypoint'], $entrypoints) ? $options['entrypoint'] : 'free';

        $options['size'] = !empty($options['size']) ? $options['size'] : 'l';


        $p = $this->wire('pages')->get($options['page']->id);
        if (!$p->id)
            throw new WireException($this->className() . _(' invalid page supplied in pagePeeker options'));

        $templateName = $options['template'];
        $tpl = $this->wire('templates')->get("name=$templateName");

        $imagesField = $options['field'];
        if (!$tpl->hasField($imagesField))
            throw new WireException($this->className() . _(' the template you supplied does not have the image field as specified in pagePeeker options'));

        $ebLiveUrl = urlencode(str_ireplace("https://www.", "", $ebResponse['url']));

        $http = $this->wire(new WireHttp());

        // http://{entrypoint}.pagepeeker.com/v2/thumbs.php?size=m&refresh=1&url=wikipedia.org
        $peekerUrlMake = 'http://' . $options['entrypoint'] . '.pagepeeker.com/v2/thumbs.php?size=' . $options['size'] . '&url=' . $ebLiveUrl;

        // Get the contents of a URL
        $response = $http->get($peekerUrlMake);
        if($response !== false) {
            $responseObj = json_decode($response);
            if ($response['error'] == 1) { // error generating thumbnail
                throw new WireException($this->className() . _(' problem generating pagepeeker thumbnail'));
            }

            // OK no errors but thumbnail not ready. Try every pagepeeker server
            $response  = $http->get('http://' . $options['entrypoint'] . '.pagepeeker.com/v2/thumbs_ready.php?size=' . $options['size'] . '&url=' . $ebLiveUrl);
            $response1 = $http->get('http://' . $options['entrypoint'] . '1.pagepeeker.com/v2/thumbs_ready.php?size=' . $options['size'] . '&url=' . $ebLiveUrl);
            $response2 = $http->get('http://' . $options['entrypoint'] . '2.pagepeeker.com/v2/thumbs_ready.php?size=' . $options['size'] . '&url=' . $ebLiveUrl);
            $response3 = $http->get('http://' . $options['entrypoint'] . '3.pagepeeker.com/v2/thumbs_ready.php?size=' . $options['size'] . '&url=' . $ebLiveUrl);
            $response4 = $http->get('http://' . $options['entrypoint'] . '4.pagepeeker.com/v2/thumbs_ready.php?size=' . $options['size'] . '&url=' . $ebLiveUrl);

            /**
             * Finally, thumbnail is ready
             * Download the image: http://{entrypoint}.pagepeeker.com/v2/thumbs.php?size=m&url=wikipedia.org
             * Check all the available pagepeeker servers to speed up the download
             **/
            if ($response) {
                $fromUrl = "http://" . $options['entrypoint'] . ".pagepeeker.com/v2/thumbs.php?size=" . $options['size'] . "&url=" . $ebLiveUrl;

            } elseif ($response1) {
                $fromUrl = "http://" . $options['entrypoint'] . "1.pagepeeker.com/v2/thumbs.php?size=" . $options['size'] . "&url=" . $ebLiveUrl;

            } elseif ($response2) {
                $fromUrl = "http://" . $options['entrypoint'] . "2.pagepeeker.com/v2/thumbs.php?size=" . $options['size'] . "&url=" . $ebLiveUrl;

            } elseif ($response3) {
                $fromUrl = "http://" . $options['entrypoint'] . "3.pagepeeker.com/v2/thumbs.php?size=" . $options['size'] . "&url=" . $ebLiveUrl;

            } elseif ($response4) {
                $fromUrl = "http://" . $options['entrypoint'] . "4.pagepeeker.com/v2/thumbs.php?size=" . $options['size'] . "&url=" . $ebLiveUrl;
            } else {
                throw new WireException('Did not get a response from PagePeeker');
            }

            $imgName = "eb-preview-" . $ebResponse['id'];
            $toFile = $this->wire('config')->paths->assets . "files/" .$p->id .'/'. $imgName . '.jpg';
            $p->of(false);
            $img = $http->download( $fromUrl,  $toFile);

            // OK we have the downloaded file, now add it to the page database info for the images field
            $imgUrl = $this->wire('config')->urls->assets . "files/" .$p->id .'/'. $imgName . '.jpg';
            $pwImage = new Pageimage($p->$imagesField, $toFile);
            $pwImage->description = $ebResponse['name']['text'];
            $pwImage->tags = "eb-preview";
            $p->$imagesField->add($pwImage, $imgUrl);

            return $img;

        } else {
            throw new WireException("HTTP request failed: " . $http->getError());
        }
    }

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...