Skip to content

Commit 20d0871

Browse files
committed
Initial commit
0 parents  commit 20d0871

6 files changed

+219
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
.idea

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#Package to connect to the ActiveCampaign 5 api
2+
3+
4+
$Mailinglist = new ActivecampaignManager('url','user','pass');
5+
6+
7+
### get a list (id,name) of all mailinglists in Active Campaign
8+
$Mailinglist->lists();
9+
10+
### publish a mailing to Active Campaign
11+
$Mailinglist->publish('Subject','From-email','From-name','Body <html>','websiteUrl','mailing list id'));
12+
13+

composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "phf/activecampaign",
3+
"description": "Package to connect to the ActiveCampaign 5 api",
4+
"authors": [
5+
{
6+
"name": "Patrick Houtman",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"require": {},
11+
"autoload": {
12+
"psr-4": {
13+
"App\\": "app/",
14+
"Phf\\Activecampaign\\": "src"
15+
}
16+
}
17+
}

src/ActivecampaignInterface.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Phf\Activecampaign;
3+
4+
interface ActivecampaignInterface
5+
{
6+
/**
7+
* Method to publish an ezine
8+
*
9+
* @param $subject
10+
* @param $senderEmail
11+
* @param $senderName
12+
* @param $content
13+
* @param $baseUrl
14+
* @param $list
15+
* @return mixed
16+
*/
17+
public function publish($subject, $senderEmail, $senderName, $content, $baseUrl, $list);
18+
19+
/**
20+
* Method get all the lists
21+
*
22+
* @return mixed
23+
*/
24+
public function lists();
25+
}

src/ActivecampaignManager.php

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

src/ActivecampaignServiceProvider.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace Phf\Activecampaign;
3+
4+
use Illuminate\Support\ServiceProvider;
5+
6+
class ActivecampaignServiceProvider extends ServiceProvider
7+
{
8+
/**
9+
* Bootstrap the application services.
10+
*
11+
* @return void
12+
*/
13+
public function boot()
14+
{
15+
//
16+
}
17+
18+
/**
19+
* Register the application services.
20+
*
21+
* @return void
22+
*/
23+
public function register()
24+
{
25+
//
26+
}
27+
}

0 commit comments

Comments
 (0)