Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 05b6197

Browse files
Adds update & create contact
Updates readme
1 parent 13d85f1 commit 05b6197

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ try {
2424
}
2525
```
2626

27+
### Handling Contacts
28+
29+
##### Creating a contact
30+
31+
```
32+
BarnebysMautic\Api::createContact($email, array $fields = [);
33+
```
34+
35+
##### Updating a contact
36+
37+
Updating a contact will do a PATCH and only update specified fields in the array.
38+
39+
```
40+
BarnebysMautic\Api::updateContact($email, array $fields = [);
41+
```
42+
43+
2744
### Sending emails
2845

2946
When sending emails you can with ease use `sendToContact` and just set the email and a templateId.

src/Api.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,41 @@ public static function sendToContact($email, $templateId, array $tokens = [], $h
9898
return $api->makeRequest($html === true ? $htmlUrl : $baseUrl, $tokens, 'POST');
9999
}
100100

101+
/**
102+
* @param $email
103+
* @param array $data
104+
* @return mixed
105+
* @throws ContactNotFoundException
106+
* @throws Exception\AuthInstanceException
107+
* @throws \Exception
108+
*/
109+
public static function updateContact($email, $data = []) {
110+
111+
$contactId = self::getContactIdByMail($email);
112+
113+
$api = self::getApiInstance();
114+
115+
$baseUrl = sprintf('contacts/%s/edit', $contactId);
116+
117+
return $api->makeRequest($baseUrl, $data, 'PATCH');
118+
}
119+
120+
/**
121+
* @param $email
122+
* @param array $data
123+
* @return mixed
124+
* @throws Exception\AuthInstanceException
125+
*/
126+
public static function createContact($email, $data = []) {
127+
128+
$api = self::getApiInstance();
129+
130+
$data = array_merge([
131+
'email' => $email,
132+
'ipAddress' => $_SERVER['REMOTE_ADDR']
133+
], $data);
134+
135+
return $api->makeRequest('contacts/new', $data, 'POST');
136+
}
137+
101138
}

0 commit comments

Comments
 (0)