forked from ActiveCampaign/activecampaign-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.php
149 lines (121 loc) · 3.47 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
<?php
require_once("includes/ActiveCampaign.class.php");
$ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY);
/*
* TEST API CREDENTIALS.
*/
if (!(int)$ac->credentials_test()) {
echo "<p>Access denied: Invalid credentials (URL and/or API key).</p>";
exit();
}
else {
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) {
// successful request
$list_id = (int)$list_add->id;
echo "<p>List added successfully (ID {$list_id})!</p>";
}
else {
// request failed
echo "<p>Adding list failed. Error returned: " . $list_add->error . "</p>";
exit();
}
/*
* ADD OR EDIT CONTACT (TO THE NEW LIST CREATED ABOVE).
*/
$contact = array(
"email" => "test@example.com",
"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) {
// successful request
$contact_id = (int)$contact_sync->subscriber_id;
echo "<p>Contact synced successfully (ID {$contact_id})!</p>";
}
else {
// request failed
echo "<p>Syncing contact failed. Error returned: " . $contact_sync->error . "</p>";
exit();
}
/*
* ADD NEW EMAIL MESSAGE (FOR A CAMPAIGN).
*/
$message = array(
"format" => "mime",
"subject" => "Check out our latest deals!",
"fromemail" => "newsletter@test.com",
"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) {
// successful request
$message_id = (int)$message_add->id;
echo "<p>Message added successfully (ID {$message_id})!</p>";
}
else {
// request failed
echo "<p>Adding email message failed. Error returned: " . $message_add->error . "</p>";
exit();
}
/*
* 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) {
// successful request
$campaign_id = (int)$campaign_create->id;
echo "<p>Campaign created and sent! (ID {$campaign_id})!</p>";
}
else {
// request failed
echo "<p>Creating campaign failed. Error returned: " . $campaign_create->error . "</p>";
exit();
}
/*
* 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>";
?>
<a href="http://www.activecampaign/api">View more API examples!</a>