|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
Raja Ongkir API |
|
5
|
|
|
Author: Irfa Ardiansyah <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Irfa\RajaOngkir\Ongkir\Func; |
|
9
|
|
|
|
|
10
|
|
|
use Exception; |
|
11
|
|
|
use Irfa\RajaOngkir\Caching\CacheCurl; |
|
12
|
|
|
|
|
13
|
|
|
class Api extends CacheCurl |
|
14
|
|
|
{ |
|
15
|
|
|
private static $account_type; |
|
16
|
|
|
private static $api_key; |
|
17
|
|
|
private static $url; |
|
18
|
|
|
private static $count = 0; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private static function setup_option() |
|
21
|
|
|
{ |
|
22
|
|
|
if (function_exists('config') and function_exists('app')) {//Load Config For Laravel |
|
|
|
|
|
|
23
|
|
|
self::$account_type = strtolower(config('irfa.rajaongkir.account_type')); |
|
24
|
|
|
self::$api_key = config('irfa.rajaongkir.api_key'); |
|
25
|
|
|
} else {//Load config For PHP Native |
|
26
|
|
|
require __DIR__.'../../../../config/config.php'; |
|
27
|
|
|
self::$account_type = strtolower($config['account_type']); |
|
|
|
|
|
|
28
|
|
|
self::$api_key = $config['api_key']; |
|
29
|
|
|
} |
|
30
|
|
|
if (self::$account_type == 'pro') { |
|
31
|
|
|
self::$url = 'https://pro.rajaongkir.com/api'; |
|
32
|
|
|
} else { |
|
33
|
|
|
self::$url = 'https://api.rajaongkir.com/'.self::$account_type; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected static function cacheProvince() |
|
38
|
|
|
{ |
|
39
|
|
|
self::setup_option(); |
|
40
|
|
|
echo "Retrieving data from \033[96m".self::$url."...\033[0m".PHP_EOL; |
|
41
|
|
|
CacheCurl::caching(self::get_province())->province(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected static function cacheCity() |
|
45
|
|
|
{ |
|
46
|
|
|
self::setup_option(); |
|
47
|
|
|
echo "Retrieving data from\033[96m ".self::$url."...\033[0m".PHP_EOL; |
|
48
|
|
|
CacheCurl::caching(self::get_city())->city(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected static function get_province($arr = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($arr != null) { |
|
54
|
|
|
$province_id = array_key_exists('province_id', $arr) ? '?id='.$arr['province_id'] : null; |
|
55
|
|
|
} else { |
|
56
|
|
|
$province_id = null; |
|
57
|
|
|
} |
|
58
|
|
|
self::setup_option(); |
|
59
|
|
|
$curl = curl_init(); |
|
60
|
|
|
curl_setopt_array($curl, [ |
|
|
|
|
|
|
61
|
|
|
CURLOPT_URL => self::$url.'/province'.$province_id, |
|
62
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
63
|
|
|
CURLOPT_ENCODING => '', |
|
64
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
65
|
|
|
CURLOPT_TIMEOUT => 30, |
|
66
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67
|
|
|
CURLOPT_CUSTOMREQUEST => 'GET', |
|
68
|
|
|
CURLOPT_HTTPHEADER => [ |
|
69
|
|
|
'key: '.self::$api_key, |
|
70
|
|
|
], |
|
71
|
|
|
]); |
|
72
|
|
|
$response = curl_exec($curl); |
|
|
|
|
|
|
73
|
|
|
$err = curl_error($curl); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
curl_close($curl); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if ($err) { |
|
78
|
|
|
echo "Can't connect to server, please check your internet connection."; |
|
79
|
|
|
exit(); |
|
|
|
|
|
|
80
|
|
|
} else { |
|
81
|
|
|
$json = json_decode($response, false)->rajaongkir; |
|
82
|
|
|
if ($json->status->code == '400') { |
|
83
|
|
|
throw new Exception($json->status->description); |
|
84
|
|
|
} else { |
|
85
|
|
|
$res = $json->results; |
|
86
|
|
|
|
|
87
|
|
|
return $res; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function get_city($arr = null) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($arr != null) { |
|
95
|
|
|
$province_id = array_key_exists('province_id', $arr) ? '?province='.$arr['province_id'] : null; |
|
96
|
|
|
} else { |
|
97
|
|
|
$province_id = null; |
|
98
|
|
|
} |
|
99
|
|
|
self::setup_option(); |
|
100
|
|
|
$curl = curl_init(); |
|
101
|
|
|
curl_setopt_array($curl, [ |
|
|
|
|
|
|
102
|
|
|
CURLOPT_URL => self::$url.'/city'.$province_id, |
|
103
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
104
|
|
|
CURLOPT_ENCODING => '', |
|
105
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
106
|
|
|
CURLOPT_TIMEOUT => 30, |
|
107
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
108
|
|
|
CURLOPT_CUSTOMREQUEST => 'GET', |
|
109
|
|
|
CURLOPT_HTTPHEADER => [ |
|
110
|
|
|
'key: '.self::$api_key, |
|
111
|
|
|
], |
|
112
|
|
|
]); |
|
113
|
|
|
$response = curl_exec($curl); |
|
|
|
|
|
|
114
|
|
|
$err = curl_error($curl); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
curl_close($curl); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
if ($err) { |
|
119
|
|
|
echo "Can't connect to server, please check your internet connection."; |
|
120
|
|
|
exit(); |
|
|
|
|
|
|
121
|
|
|
} else { |
|
122
|
|
|
$json = json_decode($response, false)->rajaongkir; |
|
123
|
|
|
if ($json->status->code == '400') { |
|
124
|
|
|
throw new Exception($json->status->description); |
|
125
|
|
|
} else { |
|
126
|
|
|
$res = $json->results; |
|
127
|
|
|
|
|
128
|
|
|
return $res; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected static function get_courier($arr) |
|
134
|
|
|
{ |
|
135
|
|
|
$origin = $arr['origin']; |
|
136
|
|
|
$destination = $arr['destination']; |
|
137
|
|
|
$weight = $arr['weight']; |
|
138
|
|
|
$courier = $arr['courier']; |
|
139
|
|
|
$res = self::curl_cost_get($origin, $destination, $weight, $courier); |
|
140
|
|
|
|
|
141
|
|
|
return $res; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected static function get_cost_details($arr) |
|
145
|
|
|
{ |
|
146
|
|
|
$origin = $arr['origin']; |
|
147
|
|
|
$destination = $arr['destination']; |
|
148
|
|
|
$weight = $arr['weight']; |
|
149
|
|
|
$courier = $arr['courier']; |
|
150
|
|
|
$res = self::curl_cost_get($origin, $destination, $weight, $courier); |
|
151
|
|
|
|
|
152
|
|
|
return $res[0]->costs; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private static function curl_cost_get($origin, $destination, $weight, $courier) |
|
156
|
|
|
{ |
|
157
|
|
|
$curl = curl_init(); |
|
158
|
|
|
|
|
159
|
|
|
curl_setopt_array($curl, self::curl_cost_option($origin, $destination, $weight, $courier)); |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
$response = curl_exec($curl); |
|
|
|
|
|
|
162
|
|
|
$err = curl_error($curl); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
curl_close($curl); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
if ($err) { |
|
167
|
|
|
echo "Can't connect to server, please check your internet connection."; |
|
168
|
|
|
exit(); |
|
|
|
|
|
|
169
|
|
|
} else { |
|
170
|
|
|
$json = json_decode($response, false)->rajaongkir; |
|
171
|
|
|
if ($json->status->code == '400') { |
|
172
|
|
|
throw new Exception($json->status->description); |
|
173
|
|
|
} else { |
|
174
|
|
|
$res = $json->results; |
|
175
|
|
|
|
|
176
|
|
|
return $res; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
private static function curl_cost_option($origin, $destination, $weight, $courier) |
|
182
|
|
|
{ |
|
183
|
|
|
self::setup_option(); |
|
184
|
|
|
|
|
185
|
|
|
return [ |
|
186
|
|
|
CURLOPT_URL => self::$url.'/cost', |
|
187
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
188
|
|
|
CURLOPT_ENCODING => '', |
|
189
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
190
|
|
|
CURLOPT_TIMEOUT => 30, |
|
191
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
192
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST', |
|
193
|
|
|
CURLOPT_POSTFIELDS => 'origin='.$origin.'&destination='.$destination.'&weight='.$weight.'&courier='.strtolower($courier), |
|
194
|
|
|
CURLOPT_HTTPHEADER => [ |
|
195
|
|
|
'content-type: application/x-www-form-urlencoded', |
|
196
|
|
|
'key: '.self::$api_key, |
|
197
|
|
|
], |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|