dev-master
9999999-dev https://slicklabs.nl/MailChimp API v3 wrapper for PHP
MIT
The Requires
- php >=5.5.0
- guzzlehttp/guzzle ~6.0
by Leo Flapper
psr-7 mailchimp abstract
MailChimp API v3 wrapper for PHP
Created with the single purpose to communicate with the MailChimp API v3 as straight forward as possible with proper error handling., (*1)
Because the simple nature of this library you can call the MailChimp API methods by the desired HTTP request method., (*2)
First of all you have to include the MailChimp class., (*3)
require 'vendor/autoload.php'; use SlickLabs\MailChimp\MailChimp; use SlickLabs\MailChimp\Exception\ResponseException; $MailChimp = new MailChimp('MAILCHIMP-API-KEY');
For retrieving the MailChimp lists, (*4)
try { $response = $MailChimp->get("lists"); echo '<pre>'; print_r($response->getBody()); echo '</pre>'; } catch(ResponseException $e) { echo $e->getStatusCode().'<br />'; echo $e->getTitle().'<br />'; echo $e->getType().'<br />'; echo $e->getDetail().'<br />'; }
Or adding a new subscriber to a list., (*5)
$args = [ "body" => [ "email_address" => "myemail@myemail.com", "status" => "pending" ] ]; try { $response = $MailChimp->post("lists/LIST-ID/members", $args); echo ''; print_r($response->getBody()); echo ''; } catch(ResponseException $e) { echo $e->getStatusCode().'<br />'; echo $e->getTitle().'<br />'; echo $e->getType().'<br />'; echo $e->getDetail().'<br />'; }
Or updating an existing subscriber of a list., (*6)
use SlickLabs\MailChimp\Subscriber; $args = [ "body" => [ "status" => "subscribed" ] ]; $subscriberHash = Subscriber::hash("myemail@myemail.com"); try { $response = $MailChimp->put('lists/LIST-ID/members/'.$subscriberHash, $args); echo ''; print_r($response->getBody()); echo ''; } catch(ResponseException $e) { echo $e->getStatusCode().'<br />'; echo $e->getTitle().'<br />'; echo $e->getType().'<br />'; echo $e->getDetail().'<br />'; }
Or deleting a subscriber of a list., (*7)
use SlickLabs\MailChimp\Subscriber; $subscriberHash = Subscriber::hash("myemail@email.com"); try { $response = $MailChimp->delete("lists/LIST-ID/members/".$subscriberHash); echo ''; print_r($response->getBody()); echo ''; } catch(ResponseException $e) { echo $e->getStatusCode().'<br />'; echo $e->getTitle().'<br />'; echo $e->getType().'<br />'; echo $e->getDetail().'<br />'; }
Add MailChimp to your composer.json
file. If you are not using Composer, you should be. It's an excellent way to manage dependencies in your PHP application., (*8)
{ "require": { "slicklabs/mailchimp": "dev-master" } }
Then at the top of your PHP script require the autoloader:, (*9)
require 'vendor/autoload.php';
This is a fairly simple wrapper, but it can be made much better by contributions from those using it. If you'd like to suggest an improvement, please raise an issue to discuss it before making your pull request., (*10)
Pull requests for bugs are more than welcome - please explain the bug you're trying to fix in the message., (*11)
MailChimp API v3 wrapper for PHP
MIT
psr-7 mailchimp abstract