forked from tuputech/tupu-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtupuclient.php
211 lines (184 loc) · 6.25 KB
/
tupuclient.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* TUPU Recognition API SDK (v1.1)
* Copyright(c)2013-2017, TUPU Technology
* http://www.tuputech.com
*/
class TupuClient
{
private $apiUrl;
private $publicKey;
private $privateKey;
private $uid;
const TupuApi = 'http://api.open.tuputech.com/v3/recognition/';
//'http://api4.open.tuputech.com/v3/recognition/'
const TupuBiApi = 'http://recognition.bi.tuputech.com/v3/recognition/';
const ErrUnsorted = -1;
const ErrWrongInput = -2;
const ErrUnrecognizedResult = -3;
const ErrWrongSignature = -4;
const ErrEmptyPrivateKey = -5;
public static function initGlobalInstance($privateKey, $apiUrl = self::TupuApi)
{
$GLOBALS["GTupuClient"] = new TupuClient($privateKey, $apiUrl);
return $GLOBALS["GTupuClient"];
}
public static function globalInstance()
{
return $GLOBALS["GTupuClient"];
}
public function __construct($privateKey, $apiUrl = self::TupuApi)
{
$this->publicKey = openssl_pkey_get_public( $this->_getTupuPublicKey() );
$this->privateKey = openssl_pkey_get_private( $privateKey );
$this->apiUrl = $apiUrl;
}
public function setUID($uid)
{
$this->uid = $uid;
}
public function recognition($secretId, $images, $tags)
{
return $this->_recognition($secretId, $images, $tags);
}
private function _recognition($secretId, $images, $tags, $CID = false, $async = false)
{
if (!is_array($images)) {
return self::ErrWrongInput;
}
if (!$this->privateKey) {
return self::ErrEmptyPrivateKey;
}
$this->images = $images;
$timestamp = time();
$nonce = rand(100, 999999);
$sign_string = $secretId . "," . $timestamp . "," . $nonce;
openssl_sign($sign_string, $signature, $this->privateKey, OPENSSL_ALGO_SHA256);
$signature = base64_encode($signature);
$data = array(
'timestamp' => $timestamp,
'nonce' => $nonce,
'signature' => $signature,
'image' => $images
);
if (is_string($this->uid)) {
$data['uid'] = $this->uid;
}
if (is_string($tags) || (is_array($tags) && count($tags) > 0)) {
$data['tag'] = $tags;
}
if (is_string($CID)) {
$data['CID'] = $CID;
}
if ($async === true) {
$data['resType'] = 'async';
}
//var_dump($data);
return $this->request($secretId, $data);
}
private function request($secretId, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiUrl . $secretId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$this->setPostfields($ch, $data);
$result = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
$result = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
return $errno;
}
}
curl_close($ch);
$data = json_decode($result, true);
if ($data) {
$signature = $data['signature'];
$json = $data['json'];
$verifyRes = openssl_verify($json, base64_decode($signature), $this->publicKey, "sha256WithRSAEncryption");
if ($verifyRes == 1) {
//verfied
return json_decode($json, true);
} else {
return self::ErrWrongSignature;
}
} else {
return self::ErrUnrecognizedResult;
}
return self::ErrUnsorted;
}
private function setPostfields($ch, $postfields, $headers = null)
{
$algos = hash_algos();
$hashAlgo = null;
foreach (array('sha1', 'md5') as $preferred) {
if (in_array($preferred, $algos)) {
$hashAlgo = $preferred;
break;
}
}
if ($hashAlgo === null) {
list($hashAlgo) = $algos;
}
$boundary =
'----------------------------' .
substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12);
$body = array();
$crlf = "\r\n";
$fields = array();
foreach ($postfields as $key => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$fields[] = array($key, $v);
}
} else {
$fields[] = array($key, $value);
}
}
foreach ($fields as $field) {
list($key, $value) = $field;
if (strpos($value, '@') === 0) {
preg_match('/^@(.*?)$/', $value, $matches);
list($dummy, $filename) = $matches;
$body[] = '--' . $boundary;
$body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"';
$body[] = 'Content-Type: application/octet-stream';
$body[] = '';
$body[] = file_get_contents($filename);
} else {
$body[] = '--' . $boundary;
$body[] = 'Content-Disposition: form-data; name="' . $key . '"';
$body[] = '';
$body[] = $value;
}
}
$body[] = '--' . $boundary . '--';
$body[] = '';
$contentType = 'multipart/form-data; boundary=' . $boundary;
$content = join($crlf, $body);
$contentLength = strlen($content);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Length: ' . $contentLength,
'Expect: 100-continue',
'Content-Type: ' . $contentType,
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
private function _getTupuPublicKey()
{
//Publish key of Tupu API
$publicKey = <<<EOF
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDyZneSY2eGnhKrArxaT6zswVH9
/EKz+CLD+38kJigWj5UaRB6dDUK9BR6YIv0M9vVQZED2650tVhS3BeX04vEFhThn
NrJguVPidufFpEh3AgdYDzOQxi06AN+CGzOXPaigTurBxZDIbdU+zmtr6a8bIBBj
WQ4v2JR/BA6gVHV5TwIDAQAB
-----END PUBLIC KEY-----
EOF;
return $publicKey;
}
}
/* End of file tupuclient.php */