-
Notifications
You must be signed in to change notification settings - Fork 76
/
examples.php
165 lines (127 loc) · 4.36 KB
/
examples.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
require_once("includes/ActiveCampaign.class.php");
$ac = new ActiveCampaign("API_URL", "API_KEY");
/*
* TEST API CREDENTIALS.
*/
if (!(int)$ac->credentials_test()) {
echo "<p>Access denied: Invalid credentials (URL and/or API key).</p>";
exit();
}
echo "<p>Credentials valid! Proceeding...</p>";
/*
* VIEW ACCOUNT DETAILS.
*/
$account = $ac->api("account/view");
echo "<pre>";
print_r($account);
echo "</pre>";
/*
* ADD NEW LIST.
*/
$list = array(
"name" => "List 3",
"sender_name" => "My Company",
"sender_addr1" => "123 S. Street",
"sender_city" => "Chicago",
"sender_zip" => "60601",
"sender_country" => "USA",
);
$list_add = $ac->api("list/add", $list);
if (!(int)$list_add->success) {
// request failed
echo "<p>Adding list failed. Error returned: " . $list_add->error . "</p>";
exit();
}
// successful request
$list_id = (int)$list_add->id;
echo "<p>List added successfully (ID {$list_id})!</p>";
/*
* ADD OR EDIT CONTACT (TO THE NEW LIST CREATED ABOVE).
*/
$contact = array(
"email" => "[email protected]",
"first_name" => "Test",
"last_name" => "Test",
"p[{$list_id}]" => $list_id,
"status[{$list_id}]" => 1, // "Active" status
);
$contact_sync = $ac->api("contact/sync", $contact);
if (!(int)$contact_sync->success) {
// request failed
echo "<p>Syncing contact failed. Error returned: " . $contact_sync->error . "</p>";
exit();
}
// successful request
$contact_id = (int)$contact_sync->subscriber_id;
echo "<p>Contact synced successfully (ID {$contact_id})!</p>";
/*
* VIEW ALL CONTACTS IN A LIST (RETURNS ID AND EMAIL).
*/
$ac->version(2);
$contacts_view = $ac->api("contact/list?listid=14&limit=500");
$ac->version(1);
/*
* ADD NEW EMAIL MESSAGE (FOR A CAMPAIGN).
*/
$message = array(
"format" => "mime",
"subject" => "Check out our latest deals!",
"fromemail" => "[email protected]",
"fromname" => "Test from API",
"html" => "<p>My email newsletter.</p>",
"p[{$list_id}]" => $list_id,
);
$message_add = $ac->api("message/add", $message);
if (!(int)$message_add->success) {
// request failed
echo "<p>Adding email message failed. Error returned: " . $message_add->error . "</p>";
exit();
}
// successful request
$message_id = (int)$message_add->id;
echo "<p>Message added successfully (ID {$message_id})!</p>";
/*
* CREATE NEW CAMPAIGN (USING THE EMAIL MESSAGE CREATED ABOVE).
*/
$campaign = array(
"type" => "single",
"name" => "July Campaign", // internal name (message subject above is what contacts see)
"sdate" => "2013-07-01 00:00:00",
"status" => 1,
"public" => 1,
"tracklinks" => "all",
"trackreads" => 1,
"htmlunsub" => 1,
"p[{$list_id}]" => $list_id,
"m[{$message_id}]" => 100, // 100 percent of subscribers
);
$campaign_create = $ac->api("campaign/create", $campaign);
if (!(int)$campaign_create->success) {
// request failed
echo "<p>Creating campaign failed. Error returned: " . $campaign_create->error . "</p>";
exit();
}
// successful request
$campaign_id = (int)$campaign_create->id;
echo "<p>Campaign created and sent! (ID {$campaign_id})!</p>";
/*
* VIEW CAMPAIGN REPORTS (FOR THE CAMPAIGN CREATED ABOVE).
*/
$campaign_report_totals = $ac->api("campaign/report/totals?campaignid={$campaign_id}");
echo "<p>Reports:</p>";
echo "<pre>";
print_r($campaign_report_totals);
echo "</pre>";
?>
<p><b>Note</b>: It can also be helpful to check our <a href="http://www.activecampaign.com/api/overview.php">API documentation</a> for the HTTP method that should be used for a particular endpoint as it can affect the format of your request.</p>
<p>Example: <pre>list_field_view</pre> GET</p>
<pre>
$ac->api("list/field/view?ids=all");
<pre>
<p>Query params appended for a GET request.</p>
<p>Example: <pre>list_field_edit</pre> POST</p>
<pre>
$ac->api("list/field/edit", array(/*POST params here.*/));
</pre>
<a href="http://www.activecampaign.com/api">View more API examples!</a>