Finding Out When to Buy World Series Tickets with PHP, Guzzle, Seat Geek API and Twilio

Born and raised in Kansas City, I’ve been waiting my whole life to see the Royals win a World Series. This year “The Boys in Blue” have made it back to the fall classic and they’re playing the team that represents my adopted home, the New York Mets. I have my Royals cap ready and I want to support KC when they play in the greatest city in the world but tickets in NYC have hit record-setting prices. Luckily there’s hope! According to Freakonomics there’s a drop in prices a day or two before the game. In this post I’ll show you how I built an app using the SeatGeek API, PHP, Guzzle and Twilio to let me know exactly when I should buy tickets to the World Series.

In the Bullpen

We’ll need a few things before we start building:

PHP >= 5.5 – We’ll be using some dependencies that require a more modern version of PHP

My Prediction: Royals in 5

We’ll be using SeatGeek to help us get data about ticket prices for the game. If you haven’t used SeatGeek before, it’s a really great website that aggregates tickets from all over the web and helps you find the best deals.

Keeping it simple let’s open this URL in a browser to see the data. I want to attend game 4 which will be happening on my favorite holiday, Halloween. Let’s look at the response we got back from the API and look for game 4:

We could load this URL in our browser again but in order to use the data we need to make our request with PHP. We’ll do this using Guzzle. Guzzle makes it super simple to send HTTP requests with PHP. Let’s start by requiring Guzzle using Composer::

Shell

1

composer require guzzlehttp/guzzle

Now that we’ve required Guzzle we will create a new file called tickets.php and initialize our Guzzle client like this:

PHP

1

2

3

4

5

<?php

require'vendor/autoload.php';

$client=newGuzzleHttpClient();

With our Guzzle client initialized, we can make a request to the SeatGeek API and take a look at the data we get back:

We’re mostly interested in two fields: average_price and lowest_price. To output their current values we can echo them:

1

2

3

4

$data=&nbsp;json_decode($res->getBody());

echo$data->stats->average_price."n";

echo$data->stats->lowest_price."n";

Let’s run our code to see where things currently stand: php tickets.php

An average price of $1,446?!? I really hope prices go down. In order to track if they do we need to store the current values. We’re only storing two values so let’s keep it simple and save them into a file:

Now that we’re saving and reading the data successfully, the next step is to compare the previous values with the current values and look for a price drop. Replace the var_dump with an if statement that runs this check:

PHP

1

2

3

4

5

6

7

8

$last_data=split(',',fread($file,filesize("data.csv")));

if($data->stats->average_price<($last_data[0]*.90)

&nbsp;&nbsp;||$data->stats->lowest_price<($last_data[1]*.90)){

&nbsp;&nbsp;echo"I better buy some tickets";

}

fclose($file);

Test this out by updating data.csv with values that will trigger our conditional (for example: 10000,10000). Then run the app again: php tickets.php

Play Ball!
It’s the bottom of the 9th and Alex Gordon is at the plate. It’s time to hammer this one home. We’re able to pull in the data and detect a price drop so now we just need to have Twilio text us when it happens. We’ll start off by using Composer to require the Twilio PHP helper library:

Shell

1

composer require twilio/sdk

We can find the code to send an SMS with PHP directly in the Twilio docs. Placing this code within our if statement will send us a text message when the price drops:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

if($data->stats->average_price<($last_data[0]*.90)

||$data->stats->lowest_price<($last_data[1]*.90)){

$account_sid='YOUR_ACCOUNT_SID';

$auth_token='YOUR_AUTH_TOKEN';

$client=newServices_Twilio($account_sid,$auth_token);

$client->account->messages->create(array(

'To'=>"YOUR_CELL_NUMBER",

'From'=>"YOUR_TWILIO_PHONE_NUMBER",

'Body'=>"Hurry! Buy Royals Tickets!"

));

}

You’ll notice we need a Twilio number in order to send our message. Jump into the phone number dashboard and grab one that looks good to you (I’d recommend one with the 816 area code for this hack). Once you buy your number plug it into the code above and you should be good to go. Make sure to add your Twilio credentials and cell phone number to this script as well.

Test it out: php tickets.php

We want this script to run every hour. We can do that really easily as a cronjob. We’ll start by editing our crontab from the command line with the crontab -e command.

Add the following line to run the code every hour:

1

0****php/path/to/tickets.php

Sit back and wait for our app to text when it’s time to buy tickets.

Go Royals!
Hope you all are rooting for the Royals in this World Series. Or if not, hope you had fun slinging some PHP with me. Here are some other ideas of how you could build onto our current application:

Update the app to send an MMS with a picture of a broom (because the Royals are going for the sweep).

Integrate the Spotify API and have the app suggest a victory song to listen to when the Royals win