· 5 years ago · Feb 24, 2021, 01:42 PM
1
2<?php
3
4// By default, this sample code is designed to get the result from your ActiveCampaign installation and print out the result
5$url = 'https://abidatidjani.api-us1.com';
6
7
8$params = array(
9
10 // the API Key can be found on the "Your Settings" page under the "API" tab.
11 // replace this with your API Key
12 'api_key' => '9552d0de20776667853bfc0b762114465a9b5b6acab87f8eb3bc3f437232569d1d91b11b',
13
14 // this is the action that adds a contact
15 'api_action' => 'contact_add',
16
17 // define the type of output you wish to get back
18 // possible values:
19 // - 'xml' : you have to write your own XML parser
20 // - 'json' : data is returned in JSON format and can be decoded with
21 // json_decode() function (included in PHP since 5.2.0)
22 // - 'serialize' : data is returned in a serialized format and can be decoded with
23 // a native unserialize() function
24 'api_output' => 'serialize',
25);
26
27// here we define the data we are posting in order to perform an update
28$post = array(
29 'email' => 'test@example.com',
30 'first_name' => 'FirstName',
31 //'ip4' => '127.0.0.1',
32
33 // any custom fields
34 //'field[345,0]' => 'field value', // where 345 is the field ID
35 //'field[%PERS_1%,0]' => 'field value', // using the personalization tag instead (make sure to encode the key)
36
37 // assign to lists:
38 'p[10]' => 123, // example list ID (REPLACE '123' WITH ACTUAL LIST ID, IE: p[5] = 5)
39 'status[10]' => 1, // 1: active, 2: unsubscribed (REPLACE '123' WITH ACTUAL LIST ID, IE: status[5] = 1)
40 //'form' => 1001, // Subscription Form ID, to inherit those redirection settings
41 //'noresponders[123]' => 1, // uncomment to set "do not send any future responders"
42 //'sdate[123]' => '2009-12-07 06:00:00', // Subscribe date for particular list - leave out to use current date/time
43 // use the folowing only if status=1
44 'instantresponders[123]' => 1, // set to 0 to if you don't want to sent instant autoresponders
45 //'lastmessage[123]' => 1, // uncomment to set "send the last broadcast campaign"
46
47 //'p[]' => 345, // some additional lists?
48 //'status[345]' => 1, // some additional lists?
49);
50
51// This section takes the input fields and converts them to the proper format
52$query = "";
53foreach( $params as $key => $value ) $query .= urlencode($key) . '=' . urlencode($value) . '&';
54$query = rtrim($query, '& ');
55
56// This section takes the input data and converts it to the proper format
57$data = "";
58foreach( $post as $key => $value ) $data .= urlencode($key) . '=' . urlencode($value) . '&';
59$data = rtrim($data, '& ');
60
61// clean up the url
62$url = rtrim($url, '/ ');
63
64// This sample code uses the CURL library for php to establish a connection,
65// submit your request, and show (print out) the response.
66if ( !function_exists('curl_init') ) die('CURL not supported. (introduced in PHP 4.0.2)');
67
68// If JSON is used, check if json_decode is present (PHP 5.2.0+)
69if ( $params['api_output'] == 'json' && !function_exists('json_decode') ) {
70 die('JSON not supported. (introduced in PHP 5.2.0)');
71}
72
73// define a final API request - GET
74$api = $url . '/admin/api.php?' . $query;
75
76$request = curl_init($api); // initiate curl object
77curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
78curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
79curl_setopt($request, CURLOPT_POSTFIELDS, $data); // use HTTP POST to send form data
80//curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment if you get no gateway response and are using HTTPS
81curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
82
83$response = (string)curl_exec($request); // execute curl post and store results in $response
84
85// additional options may be required depending upon your server configuration
86// you can find documentation on curl options at http://www.php.net/curl_setopt
87curl_close($request); // close curl object
88
89if ( !$response ) {
90 die('Nothing was returned. Do you have a connection to Email Marketing server?');
91}
92
93// This line takes the response and breaks it into an array using:
94// JSON decoder
95//$result = json_decode($response);
96// unserializer
97$result = unserialize($response);
98// XML parser...
99// ...
100
101// Result info that is always returned
102echo 'Result: ' . ( $result['result_code'] ? 'SUCCESS' : 'FAILED' ) . '<br />';
103echo 'Message: ' . $result['result_message'] . '<br />';
104
105// The entire result printed out
106echo 'The entire result printed out:<br />';
107echo '<pre>';
108print_r($result);
109echo '</pre>';
110
111// Raw response printed out
112echo 'Raw response printed out:<br />';
113echo '<pre>';
114print_r($response);
115echo '</pre>';
116
117// API URL that returned the result
118echo 'API URL that returned the result:<br />';
119echo $api;
120
121echo '<br /><br />POST params:<br />';
122echo '<pre>';
123print_r($post);
124echo '</pre>';
125
126?>