How And Why We Simplified The SparkPost PHP Client Library

Avi Goldman
Jul. 28, 2017 by Avi Goldman

You might remember that last year we released the 2.0 version of the SparkPost PHP client library. We’ve talked about the evolution of our approach across all our client libraries, but I wanted to dive deeper into the PHP changes.

Why

After 18 months of having our client libraries in the wild we’ve learned some important lessons. The biggest one was that abstraction is hard when you’re wrapping a living, growing API. With 16 API endpoints and more coming we were limiting our users and ourselves as our client libraries fell behind our API additions. We changed our mentality to focus on providing a thin layer of abstraction with some syntactic “sugar” to simplify the more complex areas of our API.

Using the PHP client library

In the new version we decided to let a single method drive all SparkPost requests. This lets you hit any API endpoint quickly and with a ton of flexibility.

In the 1.x version we did a good amount of property mapping. For example, to enable inline css you would set 'inlineCss' => true when you called $sparky->transmission->send. Now in the 2.x release, we simply pass along the data you provide, so to inline css you would set 'options' => [ 'inline_css' => true ] . While this might look more complex at first it actually makes it easier to use since you can now depend on our main API docs and quickly translate JSON into an associative array.

We also moved off of the deprecated egeloen/ivory-http-adapter to use php-http/httplug. This means you can continue to bring your own HTTP request library and adds the new ability to use promises in PHP.

How To Use It

We have a bunch of examples on how to use our new 2.x library in the github repo. Here’s a quick walk through on how to get going.

Installing & setup

First we need to pull the library from composer. If you don’t have that setup, check out their getting started page. (If you’re using a PHP version earlier than 5.6 or you can’t use composer you can still use our API with some simple cURL magic!)

We are going to use guzzle as our request library. You can use any of the httplug-supported clients.

Now that we have our dependencies we can set up the library. To run our code synchronously we’ll set async to false.

Building the transmission

Looking at the following transmission, we can see it’s almost identical to a regular SparkPost transmission. The difference is that we added a cc and bcc. This is that “sugar” I mentioned. The library will go through and format your cc and bcc recipients to fit the SparkPost API’s requirements.

And there you go! Your email is off!

To dig into the library, checkout the repo on GitHub, or come talk to our team in Slack. And if you find a bug (😱) submit an issue or a PR. Check back soon for an upcoming post on using SparkPost with some popular PHP frameworks.

Avi, Software Engineer

Dev Survival Guide Blog Footer

Related Content

SparkPost Client Libraries: Oh The Places You'll Go!

SparkPost's client libraries allow you to access our API in a native-to-them programming language. Easy peasy, lemon squeezy!

read more

Using SparkPost in PHP

A how-to guide on using our PHP client library to wrap code and use SparkPost through our API.

read more

Tracking Recipient Preferences With The User Agent Header in PHP

Learn how to build out a webhook service to receive, process, and store user agent details for tracking clicks from your SparkPost account.

read more

Start sending email in minutes!

The world’s most powerful email delivery solution is now yours in a developer-friendly, quick to set up cloud service. Open a SparkPost account today and get started for free.

Get Started

Send this to a friend