Finding Out When to Buy World Series Tickets with PHP, Guzzle, Seat Geek API and Twilio
Time to read: 4 minutes
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
- Composer – This will help us manage our dependencies
- A Twilio Account – Sign up for free
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.
To build our application we’ll be working with the /events
resource in the SeatGeek API. In order to get information about a game we need to find the id
of the game. We can get information about all upcoming Royals games at the following url:
http://api.seatgeek.com/2/events?performers.slug=kansas-city-royals
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:
The id
for the game is 2775939
. Now that we have the id
we can get the info about the game with this URL:
http://api.seatgeek.com/2/events/2775939
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::
Now that we’ve required Guzzle we will create a new file called tickets.php
and initialize our Guzzle client like this:
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:
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:
Run the code again to make sure the data is saving properly:
php tickets.php
After the code finishes running check data.csv
to make sure things saved correctly:
Above the code where we save our values, let’s add some code that reads our saved data:
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:
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:
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:
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:
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
- Have the app make a phone call to me with a reminder to not jinx Kansas City
Have any questions about PHP, Guzzle or want to show off your latest Twilio hack? Drop me a note on twitter (@rickyrobinett) or leave a comment below.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.