Skip to content

Instantly share code, notes, and snippets.

@neilmaledev
Created August 9, 2017 04:58
Show Gist options
  • Save neilmaledev/84f9d80d4a7cdb60322f541dd1455863 to your computer and use it in GitHub Desktop.
Save neilmaledev/84f9d80d4a7cdb60322f541dd1455863 to your computer and use it in GitHub Desktop.
MailChimp Php Service
<?php
namespace Services;
class MailChimp
{
protected $username;
protected $api_key;
protected $api_url;
protected $list_id;
function __construct()
{
$config = \Phalcon\DI::getDefault()->getConfig();
$this->username = $config->mailchimp->username;
$this->api_key = $config->mailchimp->api_key;
$this->list_id = $config->mailchimp->list_id;
list(, $dc) = explode('-', $this->api_key);
$this->api_url = str_replace('<dc>', $dc, $config->mailchimp->api_url);
}
/**
* This method must accomodate all incoming request to MailChimp API
* Reference: https://developer.mailchimp.com/documentation/mailchimp/reference/overview/
* @param [object] $params [Request parameters]
* "endpoint" [Required. API Url endpoint. See reference]
* "http_method" [Optional. Default value is "GET"]
* "data" [Optional. Additional/Extra data needed by the request]
* @return [object] $result [The MailChimp API response in object]
*/
public function makeRequest($params)
{
$ch = curl_init($this->api_url . $params["endpoint"]);
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, "content-type: application/json");
switch ($params["http_method"]) {
case "POST":
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params["data"]));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
break;
}
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
public function getListId()
{
return $this->list_id;
}
}
@neilmaledev
Copy link
Author

Your code must contain/include the following mailchimp config:

"mailchimp" => array(
"username" => "your-username",
"api_url" => "https://.api.mailchimp.com/3.0",
"api_key" => "yourgeneratedrandomapikey-datacenter",
"list_id" => "yourlistid123"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment