-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractClient.php
111 lines (89 loc) · 3.5 KB
/
AbstractClient.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
<?php
namespace Zackyjack\AdvanceAI;
abstract class AbstractClient
{
protected $apiHost;
protected $accessKey;
protected $secretKey;
protected $requestUrl;
protected $requestPostBody;
protected $requestHeaders;
protected $ignoreSslCheck = false;
protected $requestError;
protected $timeoutConnect = 5;
protected $timeoutReadWrite = 60;
public function __construct($api_host, $access_key, $secret_key)
{
$this->apiHost = $api_host;
$this->accessKey = $access_key;
$this->secretKey = $secret_key;
}
public function setIgnoreSslCheck($v)
{
$this->ignoreSslCheck = $v;
}
public function setTimeout($connect, $readWrite)
{
$this->timeoutConnect = $connect;
$this->timeoutReadWrite = $readWrite;
}
public function getRequestError()
{
return $this->requestError;
}
protected function prepare($api_name, $param_array, $file_array = null)
{
if (substr($api_name, 0, 1) != '/') {
$api_name = '/' . $api_name;
}
$this->requestError = null;
$this->requestUrl = substr($this->apiHost, -1) == '/' ? substr($this->apiHost, 0, -1) : $this->apiHost;
$this->requestUrl .= $api_name;
if ($file_array) {
//use multipart
$this->requestPostBody = '';
$boundary = uniqid('----AD1238MJL7' . time() . 'I', true);
$contentType = "multipart/form-data; boundary=$boundary";
if ($param_array) {
foreach ($param_array as $k => $v) {
if (!is_scalar($v)) {
throw new \RuntimeException("only scalar key/value params support when uploading files");
}
$this->requestPostBody .= "--{$boundary}\r\n";
$this->requestPostBody .= "Content-Disposition: form-data; name=\"$k\"\r\n";
$this->requestPostBody .= "\r\n{$v}\r\n";
}
}
foreach ($file_array as $k => $fn) {
if (!file_exists($fn)) {
throw new \RuntimeException("$fn not exists");
}
$baseName = basename($fn);
$mimeType = 'application/octet-stream';
$fileContent = file_get_contents($fn);
$this->requestPostBody .= "--{$boundary}\r\n";
$this->requestPostBody .= "Content-Disposition: form-data; name=\"$k\"; filename=\"$baseName\"\r\n";
$this->requestPostBody .= "Content-Type: $mimeType\r\n";
$this->requestPostBody .= "\r\n{$fileContent}\r\n";
}
$this->requestPostBody .= "--{$boundary}--";
} else {
//use json
$contentType = 'application/json';
$this->requestPostBody = json_encode($param_array);
}
$now = gmdate('D, d M Y H:i:s', time()) . ' GMT';
$this->requestHeaders = array(
'Content-Type: ' . $contentType,
'Date: ' . $now,
);
$separator = '$';
$sign_str = 'POST' . $separator;
$sign_str .= $api_name . $separator;
$sign_str .= $contentType . $separator;
$sign_str .= $now . $separator;
$authorization = sprintf('%s:%s', $this->accessKey, base64_encode(hash_hmac('sha256', $sign_str, $this->secretKey, true)));
$this->requestHeaders[] = 'Authorization:' . $authorization;
}
abstract public function request($api_name, $param_array);
}