Back

Step by Step Guide to Building a Sendy API Integration in PHP

Aug 17, 20245 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your email marketing with Sendy? Let's dive into building a robust API integration using the nifty sendynl/php-sdk package. This guide assumes you're already familiar with PHP and are looking for a quick, no-nonsense approach to getting things up and running.

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment (you're a pro, so I'm sure you've got this covered)
  • Composer installed (because who doesn't love dependency management?)
  • Your Sendy API credentials (if you don't have these, go grab them from your Sendy dashboard)

Installation

First things first, let's get that SDK installed. Fire up your terminal and run:

composer require sendynl/php-sdk

Easy peasy, right?

Configuration

Now, let's set up those API credentials and get our Sendy client ready to roll:

use Sendy\SendyPhpSdk\Sendy; $sendy = new Sendy('YOUR_API_KEY', 'https://your-sendy-installation.com');

Basic Operations

Subscribing a User

Let's add someone to your list:

$result = $sendy->subscribe('list_id', '[email protected]', 'John Doe');

Unsubscribing a User

Parting ways? No problem:

$result = $sendy->unsubscribe('list_id', '[email protected]');

Checking Subscription Status

Curious about a user's status?

$status = $sendy->getSubscriptionStatus('list_id', '[email protected]');

Advanced Operations

Creating a Campaign

Time to craft that perfect email:

$campaign = $sendy->createCampaign([ 'from_name' => 'Your Name', 'from_email' => '[email protected]', 'subject' => 'Check out our latest features!', 'plain_text' => 'Hello, world!', 'html_text' => '<h1>Hello, world!</h1>', 'list_ids' => 'list_id1, list_id2' ]);

Sending a Campaign

Ready to hit send?

$result = $sendy->sendCampaign($campaign['campaign_id']);

Retrieving Campaign Stats

Let's see how that campaign performed:

$stats = $sendy->getCampaignStats($campaign['campaign_id']);

Error Handling

Don't let those pesky errors catch you off guard. Wrap your API calls in try-catch blocks:

try { $result = $sendy->subscribe('list_id', '[email protected]', 'John Doe'); } catch (\Exception $e) { // Handle the error gracefully error_log($e->getMessage()); }

Best Practices

  • Respect Sendy's rate limits. No one likes a spammer!
  • Keep those API credentials safe. Use environment variables or a secure config file.

Testing

Unit testing is your friend. Mock those API responses:

use PHPUnit\Framework\TestCase; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; class SendyTest extends TestCase { public function testSubscribe() { $mock = new MockHandler([ new Response(200, [], json_encode(['status' => true])) ]); $client = new Client(['handler' => $mock]); $sendy = new Sendy('fake_api_key', 'https://fake-url.com', $client); $result = $sendy->subscribe('list_id', '[email protected]', 'Test User'); $this->assertTrue($result); } }

Conclusion

And there you have it! You're now armed with the knowledge to build a solid Sendy API integration. Remember, the sendynl/php-sdk package is your trusty sidekick in this journey. Don't be afraid to dive into the source code or hit up the documentation for more advanced features.

Now go forth and conquer those email campaigns! Happy coding!