Skip to content
This repository was archived by the owner on Jun 2, 2026. It is now read-only.

Commit 6f12e2b

Browse files
authored
Merge pull request #1 from HiccupInsurance/add-member-api
Add member api
2 parents 75df0e5 + 51b5886 commit 6f12e2b

6 files changed

Lines changed: 135 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
composer.lock
3+
config.yml

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
"require": {
1818
"php": ">=7.0.1",
1919
"guzzlehttp/guzzle": "^6.3"
20+
},
21+
"require-dev": {
22+
"hassankhan/config": "^0.11.2",
23+
"symfony/yaml": "^3.3"
2024
}
2125
}

smoke-test/member.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require(__DIR__ . '/../vendor/autoload.php');
4+
5+
use Hiccup\MailChimpApi\MailChimp;
6+
use Noodlehaus\Config;
7+
8+
$config = new Config(__DIR__ . '/../config.yml');
9+
$mailChimp = new MailChimp($config->get('api_key'));
10+
11+
$response = $mailChimp->getMember()->subscribe(
12+
$config->get('list_id'),
13+
[
14+
'email_address' => 'budi.arsana@hiccup.co'
15+
]
16+
);
17+
18+
var_dump($response);

src/Api/Member.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Hiccup\MailChimpApi\Api;
44

55
use GuzzleHttp\Client;
6+
use GuzzleHttp\RequestOptions;
7+
use Hiccup\MailChimpApi\Exception\InvalidParametersException;
8+
use Hiccup\MailChimpApi\MailChimp;
69

710
/**
811
* Class Member
@@ -48,15 +51,78 @@ public function __construct(Client $client)
4851
/**
4952
* @param string $listId
5053
* @param array $parameters see MailChimp doc for list of available parameters
54+
* @return array
55+
* @throws InvalidParametersException
56+
*/
57+
public function subscribe(string $listId, array $parameters): array
58+
{
59+
if (isset($parameters['email_address']) === false) {
60+
throw new InvalidParametersException(['email_address' => 'required']);
61+
}
62+
63+
$parameters['status_if_new'] = Member::STATUS_SUBSCRIBED;
64+
$parameters['status'] = Member::STATUS_SUBSCRIBED;
65+
$member = $this->put($listId, $parameters);
66+
67+
return $member;
68+
}
69+
70+
/**
71+
* @param string $listId
72+
* @param array $parameters
73+
* @return array
74+
* @throws InvalidParametersException
75+
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash
76+
*/
77+
public function put(string $listId, array $parameters): array
78+
{
79+
if (isset($parameters['email_address']) === false) {
80+
throw new InvalidParametersException(['email_address' => 'required']);
81+
}
82+
83+
$response = $this->client->put(
84+
sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($parameters['email_address'])),
85+
[RequestOptions::JSON => $parameters]
86+
);
87+
88+
return json_decode((string) $response->getBody(), true);
89+
}
90+
91+
/**
92+
* @param string $listId
93+
* @param array $parameters
94+
* @return array
95+
* @throws InvalidParametersException
96+
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash
97+
*/
98+
public function patch(string $listId, array $parameters): array
99+
{
100+
if (isset($parameters['email_address']) === false) {
101+
throw new InvalidParametersException(['email_address' => 'required']);
102+
}
103+
104+
$response = $this->client->patch(
105+
sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($parameters['email_address'])),
106+
[RequestOptions::JSON => $parameters]
107+
);
108+
109+
return json_decode((string) $response->getBody(), true);
110+
}
111+
112+
/**
113+
* @param string $listId
114+
* @param string $email
51115
*
52116
* @return array
53117
*
54-
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
118+
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
55119
*/
56-
public function create(string $listId, array $parameters): array
120+
public function get(string $listId, string $email): array
57121
{
58-
$response = $this->client->post(sprintf('/lists/%s/members', $listId), $parameters);
122+
$response = $this->client->get(
123+
sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($email))
124+
);
59125

60-
return json_decode((string) $response, true);
126+
return json_decode((string) $response->getBody(), true);
61127
}
62128
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Hiccup\MailChimpApi\Exception;
4+
5+
use Throwable;
6+
7+
final class InvalidParametersException extends \Exception
8+
{
9+
10+
/**
11+
* @param array $errors
12+
* @param int $code
13+
* @param Throwable|null $previous
14+
*/
15+
public function __construct(array $errors, $code = 400, Throwable $previous = null)
16+
{
17+
parent::__construct($this->buildMessage($errors), $code, $previous);
18+
}
19+
20+
/**
21+
* @param array $errors
22+
* @return string
23+
*/
24+
private function buildMessage(array $errors): string
25+
{
26+
$listMessages = [];
27+
28+
foreach ($errors as $parameter => $message) {
29+
$listMessages[] = sprintf('Parameter "%s" is "%s"', $parameter, $message);
30+
}
31+
32+
return implode('. ', $listMessages);
33+
}
34+
}

src/MailChimp.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
final class MailChimp
1313
{
1414

15+
#----------------------------------------------------------------------------------------------
16+
# Constants
17+
#----------------------------------------------------------------------------------------------
18+
19+
const VERSION = '3.0';
20+
1521
#----------------------------------------------------------------------------------------------
1622
# Properties
1723
#----------------------------------------------------------------------------------------------
@@ -30,7 +36,7 @@ public function __construct(string $apiKey)
3036
$dataCenter = explode('-', $apiKey)[1];
3137

3238
$client = new Client([
33-
'base_uri' => sprintf('https://%s.api.mailchimp.com/3.0', $dataCenter),
39+
'base_uri' => sprintf('https://%s.api.mailchimp.com', $dataCenter),
3440
'auth' => ['any', $apiKey]
3541
]);
3642

@@ -44,7 +50,7 @@ public function __construct(string $apiKey)
4450
/**
4551
* @return Member
4652
*/
47-
public function member(): Member
53+
public function getMember(): Member
4854
{
4955
return $this->member;
5056
}

0 commit comments

Comments
 (0)