This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommissionApiClass.php
120 lines (73 loc) · 2.59 KB
/
CommissionApiClass.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
<?php
class CommissionApiException extends Exception
{
private $params;
public function __construct($message, $code = 0, $params = array(), Exception $previous = null) {
$this->params = $params;
parent::__construct($message, $code, $previous);
}
public function getParams(){
return $this->params;
}
}
class CommissionApi {
//Email
private $login = "<you email>";
//Password
private $password = "<you password>";
private $access_token;
private $auth_url = "https://my.fbs.com/api/v1/user/login?client_token=commission";
private $commission_url = "https://my.fbs.com/api/v1/commission/list";
public function getCommissionOrders($date, $account){
if(!isset($this->access_token)) $this->getToken();
$querydata = array(
"access_token" => $this->access_token,
"date" => $date,
"account" => $account
);
return $this->sendGetRequest($this->commission_url, $querydata);
}
private function getToken()
{
$postdata = array(
'username' => $this->login,
'password' => $this->password,
);
$data = $this->sendPostRequest($this->auth_url, $postdata);
$this->access_token = $data->access_token;
}
private function sendGetRequest($url, $querydata = array())
{
return $this->sendRequest($url, $querydata, "GET");
}
private function sendPostRequest($url, $postdata)
{
return $this->sendRequest($url, $postdata, "POST");
}
private function sendRequest($url, $data, $method){
$uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
$querydata = http_build_query($data);
$ch = curl_init($url);
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $querydata);
break;
case 'GET':
if($querydata !== "") $url = $url . "?" . $querydata;
break;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $this->parseResonse($result, $info);
}
protected function parseResonse($response, $info){
$res_data = json_decode($response);
if(!isset($res_data)) throw new CommissionApiException("Wrong result", $info["http_code"], array('data' => $response));
if(isset($res_data->error)) throw new CommissionApiException($res_data->error, $info["http_code"], isset($res_data->params) ? $res_data->params : null);
return $res_data->result;
}
}