This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APSIS.class.php
135 lines (110 loc) · 4.01 KB
/
APSIS.class.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
<?php
// Apsis Newsletter Pro v2 - PHP5 example code
// Include NUSOAP classes
@require_once(libraries_get_path('nusoap') .'/nusoap.php');
// Exceptions
class AnpSoapClientException extends Exception
{
public function __construct($message = null, $code = 0)
{
parent::__construct($message, $code);
}
}
// NUSOAP wrapper for ANP API
class AnpSoapclient
{
protected $service_username = null, $service_password = null;
protected $soap_client = null;
public function __construct($username, $password, $wsdl = 'http://api.anpdm.com/ExternalAPIService.asmx?wsdl')
{
// Init soapclient
$this->soap_client = $soap_client = new nusoap_client($wsdl, 'wsdl');
if(($soap_error = $soap_client->getError())) $this->ThrowError($soap_error, -1, true);
// Set authentication
$this->SetAuthenticationDetails($username, $password);
}
// public array Call( string method, array arguments )
// @method - the method to call
// @arguments - assoc array of arguments ex; ('Parameter1' => 'Value1', 'Parameter2' => 'Value2') ..
public function Call($method, $arguments = array())
{
// Init
$soap_client = $this->soap_client;
// Append anp username and password to parameters vector
$arguments = array_merge(array('strUsername' => $this->service_username, 'strPassword' => $this->service_password), (array) $arguments);
$soap_result = @$soap_client->Call($method, array('parameters' => $arguments), '', '', false, true);
// Print error/return result
return ($soap_client->fault ? $this->ThrowError($soap_result, -2, true) :
(($soap_error = $soap_client->getError()) ? $this->ThrowError($soap_error, -3, true) : $soap_result));
}
protected function ThrowError($description, $error_code = -1, $dump_description = false)
{
if($dump_description)
{
@ob_start(); print_r($description);
$description = @str_replace(chr(32) . chr(32), " ", @nl2br(@ob_get_clean()));
}
throw new AnpSoapClientException($description, $error_code);
}
protected function SetAuthenticationDetails($username, $password)
{
if((strlen($username) == 0) || (strlen($password) == 0) || $username == null || $password == null)
$this->ThrowError("Username or Password empty!", -5);
$this->service_username = $username;
$this->service_password = $password;
}
}
// Demographics class
class AnpDemographicsData extends BaseDictionary
{
public function __construct($demographics_data = null)
{
// Call base constructor
parent::__construct();
// Populate demographics data
if($demographics_data != null && count($demographics_data) > 0)
foreach($demographics_data as $key => $val) $this->Add($key, $val);
}
public function GetKeys($delim = ';')
{
return implode($delim, array_keys($this->dictionary));
}
public function GetValues($delim = ';')
{
return implode($delim, array_values($this->dictionary));
}
public function Remove($key)
{
parent::Remove("DD" . $key);
}
public function Add($key, $value)
{
parent::Add("DD" . $key, $value);
}
}
class BaseDictionary
{
protected $dictionary = null;
public function __construct()
{
$this->dictionary = array();
}
public function Add($key, $value)
{
if($this->ContainsKey($key)) throw new Exception("Dictionary already contains key {$key}");
$this->dictionary[$key] = $value;
}
public function Remove($key)
{
unset($this->dictionary[$key]);
}
public function Get($key)
{
return $this->dictionary[$key];
}
public function ContainsKey($key)
{
return array_key_exists($key, $this->dictionary);
}
}
?>