Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Php example #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# MailChimp API v3.0 Examples for PHP

These examples have been tested with PHP version 5.6 and requires the cURL library.

Place your API Key in the example.php file.
18 changes: 18 additions & 0 deletions php/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
require_once(__DIR__.'/mailchimp.php');

// Init mailchimp
$mailchimp = new MailChimp('YOUR API KEY');

$reply = $mailchimp->list_add_subscriber([
'id_list' => 'ID_LIST',
'email' => 'EMAIL',
'status' => 'STATUS',
'merge_fields' => [
'FNAME' => 'FIRST NAME',
],
]);

echo '<pre>';
print_r($reply);
echo '</pre>';
132 changes: 132 additions & 0 deletions php/mailchimp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

class MailChimpException extends Exception { };

/**
* Class MailChimp
*/

class MailChimp {

private $api_uri;
private $api_user = 'api_v3';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this value should never change at run time, it should probably be a class constant:

const API_USER = 'api_v3';

Access with:

self::API_USER

Reference:

private $api_key = '';
private $user_agent = 'API V3 - PHP Sample';

public function __construct($api_key)
{
$domain = explode('-', $api_key)[1];
$this->api_uri = 'https://' . $domain . '.api.mailchimp.com/3.0';
$this->api_key = $api_key;
}

private function _execute_request($method, $endpoint, Array $payload = [])
{
$endpoint = $this->api_uri.$endpoint;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest spaces around the concatenation operator to be consistent with code in other parts of this app:

$endpoint = $this->api_uri . $endpoint;


$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, $this->api_user.':'.$this->api_key);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In reference to the use of a constant rather than a class property. Change to:

curl_setopt($ch, CURLOPT_USERPWD, self::API_USER . ':' . $this->api_key);

curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);

switch ($method)
{
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
break;
case 'GET':
$endpoint .= '?' . http_build_query($payload);
break;
case 'DELETE':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You listed the DELETE case twice here.

$endpoint .= '?' . http_build_query($payload);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
break;
}

curl_setopt($ch, CURLOPT_URL, $endpoint);
$result = curl_exec($ch);

return json_decode($result);
}

/**
* List: Add a new subscriber
* @docs http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
* @param array $options
* @return mixed
* @throws MailChimpException
*
* Usage Example:
* $obj->list_add_subscriber([
* 'id_list' => 'ID_LIST', // Required
* 'email' => 'EMAIL', // Required
* 'status' => 'STATUS', // Optional
* 'merge_fields' => [ // Optional
* 'FNAME' => 'NAME',
* ...
* ],
* ]);
*
*/
public function list_add_subscriber(Array $options)
{
// Check required params
if ( ! isset($options['id_list']) OR ! isset($options['email']))
{
throw new MailChimpException('Parameters not set on '.__METHOD__);
}

$endpoint = '/lists/' . $options['id_list'] . '/members/';
$payload = [
'email_address' => $options['email'],
'status' => isset($options['status']) ? $options['status'] : 'pending',
];
if (isset($options['merge_fields']) AND $options['merge_fields'])
{
$payload['merge_fields'] = $options['merge_fields'];
}

return $this->_execute_request('POST', $endpoint, $payload);
}


/**
* Campaign: Send a campaign
* @docs http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_send
* @param array $options
* @return mixed
* @throws MailChimpException
*
* Usage Example:
* $obj->campaign_send([
* 'id_campaign' => 'ID_CAMPAIGN', // Required
* ]);
*
*/
public function campaign_send(Array $options)
{
// Check required params
if ( ! isset($options['id_campaign']))
{
throw new MailChimpException('Parameters not set on '.__METHOD__);
}

$endpoint = '/campaigns/' . $options['id_campaign'] . '/actions/send/';

return $this->_execute_request('POST', $endpoint);
}
}

?>