-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathUps_Base.php
More file actions
181 lines (143 loc) · 3.26 KB
/
Ups_Base.php
File metadata and controls
181 lines (143 loc) · 3.26 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* Base UPS Class
*
* @author Justin Kimbrell
* @copyright Copyright (c) 2012, Objective HTML
* @link http://www.objectivehtml.com/
* @version 0.2.0
* @build 20120823
*/
abstract class Base_Ups
{
/**
* Base API URL
*
* @var string
*/
private $base_url = "https://www.ups.com/ups.app/xml";
/**
* USP Account Access Key
*
* @var string
*/
protected $access_key;
/**
* USP Account Username
*
* @var string
*/
protected $username;
/**
* USP Account Password
*
* @var string
*/
protected $password;
/**
* UPS Account Number
*
* @var string
*/
protected $account_number;
/**
* Contructor
*
* @access public
* @param array Pass object properties as array keys to set default values
* @return void
*/
public function __construct($data = array())
{
foreach($data as $key => $value)
{
if(property_exists($this, $key))
{
$this->$key = $value;
}
}
return;
}
/**
* Dynamic create setter/getter methods
*
* @access public
* @param string method name to call
* @param array arguments in the form of an array
* @return mixed
*/
public function __call($method, $args)
{
foreach(array('/^get_/' => 'get_' , '/^set_/' => 'set_') as $regex => $replace)
{
if(preg_match($regex, $method))
{
$property = str_replace($replace, '', $method);
$method = rtrim($replace, '_');
}
}
$args = array_merge(array($property), $args);
return call_user_func_array(array($this, $method), $args);
}
/**
* CURL Helper
*
* @access protected
* @param string The API endpoint in which to send request
* @param string The data to pass in the request
* @return object;
*/
protected function curl($endpoint, $data)
{
$ch = curl_init($this->url($endpoint));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return curl_exec($ch);
}
/**
* Get the value of a defined property
*
* @access public
* @param string propery name
* @return mixed
*/
public function get($prop)
{
if(isset($this->$prop))
{
return $this->$prop;
}
return NULL;
}
/**
* Set the value of a defined property
*
* @access public
* @param string propery name
* @param string propery value
* @return mixed
*/
public function set($prop, $value)
{
if(isset($this->$prop))
{
$this->$prop = $value;
}
}
/**
* Helper function to build API URL's from endpoints.
*
* @access protected
* @param string The API endpoint
* @return string
*/
protected function url($endpoint)
{
return rtrim($this->base_url, '/') . '/' . ltrim($endpoint, '/');
}
}