-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRussianPostSdk.php
More file actions
126 lines (102 loc) · 3.45 KB
/
RussianPostSdk.php
File metadata and controls
126 lines (102 loc) · 3.45 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
<?php
namespace Sdk;
use Exception;
class RussianPostSdk
{
static private $apiKey;
static private $password;
static private $url;
/**
* Sets api url, apiKey and password
* @param $url
* @param $api_key
* @param $password
* @throws Exception
*/
static public function init($url, $api_key, $password=null)
{
if(is_string($api_key) && is_string($password) && is_string($url)){
self::$apiKey = $api_key;
self::$password = $password;
self::$url = $url;
} else {
throw new Exception('URL, apiKey and password might be a string!');
}
}
/**
* @param $from_index
* @param $to_index
* @param $weight
* @param $ob_cennost_rub
* @return mixed
* @throws Exception
*/
static public function calc($from_index, $to_index, $weight, $ob_cennost_rub)
{
$params = [
"from_index"=>$from_index,
"to_index"=>$to_index,
"weight"=>$weight,
"ob_cennost_rub"=>$ob_cennost_rub
];
return self::call(__FUNCTION__,$params);
}
/**
* @param $method
* @param $params
* @return mixed
* @throws Exception
*/
static private function call($method , $params)
{
if(!extension_loaded('curl')){
throw new Exception('You have to enable curl extension!');
}
if(empty(self::$apiKey) || empty(self::$url)){
throw new Exception('apiKey and url can not be blank! Use init() method.');
}
$params["apikey"] = self::$apiKey;
$params["method"] = $method;
if (!empty(self::$password)) {
//если пароль указан, аутентификация по методу API ключ + API пароль.
$all_to_md5 = $params;
$all_to_md5[] = self::$password;
$hash = md5(implode("|", $all_to_md5));
$params["hash"] = $hash;
}
try{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::$url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
if($response === false) {
throw new Exception("10000 server error");
}
$data = json_decode($response, true);
if (isset($data['msg']['type']) && $data['msg']['type'] == "error") {
// throw new Exception(print_r($data, true));
throw new Exception('ERROR! ' . $data['msg']['text']);
}
return $data;
} catch(Exception $e){
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
public static function alternativeCalc($from_index, $to_index, $weight, $ob_cennost_rub)
{
$url = 'http://api.print-post.com/api/sendprice/v2/?weight=' . $weight . '&summ='. $ob_cennost_rub . '&from_index=' . $from_index . '&to_index=' . $to_index;
if( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
$out = curl_exec($curl);
$data = json_decode($out, true);
curl_close($curl);
return $data;
} else{
return 'error';
}
}
}