1
+ <?php
2
+ namespace Phf \Activecampaign ;
3
+
4
+ class ActivecampaignManager implements ActivecampaignInterface
5
+ {
6
+ private $ url ;
7
+ private $ user ;
8
+ private $ pass ;
9
+
10
+ public function __construct ($ url , $ user , $ pass )
11
+ {
12
+ $ this ->url = $ url ;
13
+ $ this ->user = $ user ;
14
+ $ this ->pass = $ pass ;
15
+ }
16
+
17
+ /**
18
+ * Method to publish an ezine
19
+ *
20
+ * @param $subject
21
+ * @param $senderEmail
22
+ * @param $senderName
23
+ * @param $content
24
+ * @param $baseUrl
25
+ * @param $list
26
+ * @return mixed
27
+ */
28
+ public function publish ($ subject , $ senderEmail , $ senderName , $ content , $ baseUrl , $ list )
29
+ {
30
+ $ params = [
31
+ 'api_user ' => $ this ->user ,
32
+ 'api_pass ' => $ this ->pass ,
33
+ 'api_action ' => 'message_add ' ,
34
+ 'api_output ' => 'serialize ' ,
35
+ ];
36
+
37
+ $ post = [
38
+ //'id' => 0, // adds a new one
39
+ 'format ' => 'mime ' ,
40
+ 'subject ' => $ subject ,
41
+ 'fromemail ' => $ senderEmail ,
42
+ 'fromname ' => $ senderName ,
43
+ 'reply2 ' => $ senderEmail ,
44
+ 'priority ' => '3 ' , // 1=high, 3=medium/default, 5=low
45
+ 'charset ' => 'utf-8 ' ,
46
+ 'encoding ' => '8bit ' ,
47
+
48
+ // html version
49
+ 'htmlconstructor ' => 'editor ' , // possible values: editor, external, upload
50
+ 'html ' => $ content , // content of your html email
51
+ 'htmlfetch ' => $ baseUrl , // URL where to fetch the body from
52
+ 'htmlfetchwhen ' => 'send ' , // possible values: (fetch at) 'send' and (fetch) 'pers'(onalized)
53
+
54
+ // text version
55
+ 'textconstructor ' => 'editor ' , // possible values: editor, external, upload
56
+ 'text ' => strip_tags ($ content ), // content of your text only email
57
+ 'textfetch ' => $ baseUrl , // URL where to fetch the body from
58
+ 'textfetchwhen ' => 'send ' , // possible values: (fetch at) 'send' and (fetch) 'pers'(onalized)
59
+
60
+ // assign to lists:
61
+ 'p[] ' => $ list ,
62
+ ];
63
+
64
+ $ query = "" ;
65
+ foreach ($ params as $ key => $ value ) {
66
+ $ query .= $ key . '= ' . urlencode ($ value ) . '& ' ;
67
+ }
68
+ $ query = rtrim ($ query , '& ' );
69
+
70
+ $ data = "" ;
71
+ foreach ($ post as $ key => $ value ) {
72
+ $ data .= $ key . '= ' . urlencode ($ value ) . '& ' ;
73
+ }
74
+
75
+ $ data = rtrim ($ data , '& ' );
76
+ $ url = rtrim ($ this ->url , '/ ' );
77
+ $ api = $ url . '/admin/api.php? ' . $ query ;
78
+
79
+ $ request = curl_init ($ api );
80
+ curl_setopt ($ request , CURLOPT_HEADER , 0 );
81
+ curl_setopt ($ request , CURLOPT_RETURNTRANSFER , 1 );
82
+ curl_setopt ($ request , CURLOPT_POSTFIELDS , $ data );
83
+
84
+ $ response = (string )curl_exec ($ request );
85
+ curl_close ($ request );
86
+
87
+ if (!$ response ) {
88
+ return "connection failed " ;
89
+ }
90
+
91
+ $ result = unserialize ($ response );
92
+
93
+ return ($ result ['result_code ' ] ? $ result ['id ' ] : 'FAILED ' );
94
+ }
95
+
96
+
97
+ /**
98
+ * Method get all the lists
99
+ *
100
+ * @return mixed
101
+ */
102
+ public function lists ()
103
+ {
104
+ $ params = [
105
+ 'api_user ' => $ this ->user ,
106
+ 'api_pass ' => $ this ->pass ,
107
+ 'api_action ' => 'list_list ' ,
108
+ 'ids ' => 'all_with_name ' ,
109
+ ];
110
+
111
+ $ query = "" ;
112
+ foreach ($ params as $ key => $ value ) {
113
+ $ query .= $ key . '= ' . urlencode ($ value ) . '& ' ;
114
+ }
115
+ $ query = rtrim ($ query , '& ' );
116
+
117
+ $ url = rtrim ($ this ->url , '/ ' );
118
+ $ api = $ url . '/admin/api.php? ' . $ query ;
119
+
120
+ $ request = curl_init ($ api );
121
+ curl_setopt ($ request , CURLOPT_HEADER , 0 );
122
+ curl_setopt ($ request , CURLOPT_RETURNTRANSFER , 1 );
123
+
124
+ $ response = (string )curl_exec ($ request );
125
+ curl_close ($ request );
126
+
127
+ if (!$ response ) {
128
+ return "connection failed " ;
129
+ }
130
+
131
+ $ xml = (array )simplexml_load_string ($ response , "SimpleXMLElement " , LIBXML_NOCDATA );
132
+
133
+ return empty ($ xml ) ? "FAILED " : $ xml ['row ' ];
134
+ }
135
+ }
0 commit comments