CheckActiveHelper.php
12.9 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace common\helpers;
use Exception;
use function curl_init;
use function curl_setopt;
use function stripos;
use function http_build_query;
use function curl_exec;
use function curl_getinfo;
use function in_array;
use function curl_close;
use function json_decode;
use function file_get_contents;
use function base64_encode;
class CheckActiveHelper
{
const CHECK_URL = 'http://check1.bosch-smartlife.com/qrv_zhoundlee';
const ACTIVE_URL = 'http://check1.bosch-smartlife.com/app_activate';
const APP_CHECK = 'http://check1.bosch-smartlife.com/app_check';
/**
* 调用智纹保校验接口
* @param $img 图片内容 file 是 用户上传的图片,文件类型(二进制图片数据)
* @param $fileName 图片名称 string 是 用户上传的图片原始名字
* @param $token string 是 访问令牌
* 格式: IMG_防伪标签uuid_时间戳.jpg 注意: 1) 中间两个下划线 _ 不可省略 2) 微信扫码出来的uuid参数中的版本号部分去掉@v1.0
* @return bool|string ""
* content object 内容
* status string 状态码【待定】(authentic:正品 fake:假货 其他状态码:请闵总帮忙补充)
* imgUrl file 返回的图片状态 备注:请返回完整的URL路径,例如:http://check1.bosch-smartlife.com/images/success.png
*/
public static function checkAppQrv_old($imageFullPath, $fileName, $token)
{
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::CHECK_URL);
// 获取图片内容
$filename_root = $_SERVER['DOCUMENT_ROOT'];
$path = $filename_root. '/tmp/'. date("Ymd");
$saveFilePath = $path.'/'.$fileName;
// 设置post数据
$post_data = array(
'fileName' => $fileName,
'token' => $token
);
$file = realpath($saveFilePath);
$post_data['img'] = file_get_contents($file);
Log::DEBUG("请求checkAppQrv参数:" . json_encode($post_data));
curl_setopt($curl, CURLOPT_HEADER, 0);
// 如果是https协议
if (stripos(self::CHECK_URL, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//CURL_SSLVERSION_TLSv1
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
}
// 通过POST方式提交
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
// 超时时间
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 返回内容
$callbcak = curl_exec($curl);
//http状态码
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// 处理返回状态
if (in_array($httpCode, array(400, 403)))
return "";
// 关闭,释放资源
curl_close($curl);
//@unlink($saveFilePath);
//返回内容JSON_DECODE
return json_decode($callbcak, true);
} catch (Exception $e) {
Log::DEBUG("检查图片出错:" . $e->getTraceAsString());
return false;
}
}
/**
* http post请求--CURL模拟表单上传文件
* @param $url string 请求地址
* @param $params array 请求参数
* @param $header array 请求头
* @return mixed
*/
public static function checkAppQrv($imageFullPath, $fileName, $token)
{
try {
// 获取图片内容
$filename_root = $_SERVER['DOCUMENT_ROOT'];
$path = $filename_root. '/tmp/'. date("Ymd");
$saveFilePath = $path.'/'.$fileName;
//以下代码适合PHP7.x PHP5.6
$file = new \CURLFile(realpath($saveFilePath),'multipart/form-data', 'img');
//或者使用下面过程化的写法
//$file = curl_file_create('@' .realpath($saveFilePath),'multipart/form-data');
$postfields = [
'fileName' => $fileName,
//'token' => $token,
'img' => $file
];
Log::DEBUG("请求checkAppQrv参数:" . json_encode($postfields));
$url = self::CHECK_URL;
//----------------------------
$ch = curl_init();
$params[CURLOPT_URL] = $url; //请求url地址
$params[CURLOPT_HEADER] = true; //是否返回响应头信息
$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
$params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
$params[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $postfields;
$header = array('Content-Type: multipart/form-data'); //请求头记得变化-不同的上传方式
$params[CURLOPT_HTTPHEADER] = $header;
// 超时时间
$params[CURLOPT_CONNECTTIMEOUT] = 30;
$params[CURLOPT_TIMEOUT] = 30;
curl_setopt_array($ch, $params); //传入curl参数
$content = curl_exec($ch); //执行
//http状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Log::DEBUG("checkAppQrv返回码:" .$httpCode);
if ($httpCode == 200) {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
Log::DEBUG("checkAppQrv返回headerSize:" .$headerSize);
$header = substr($content, 0, $headerSize);
$content = substr($content, $headerSize);
}
//打印请求的header信息
//$request_header = curl_getinfo($ch, CURLINFO_HEADER_OUT);
//状态处理
if (in_array($httpCode, array(400, 403)))
return "";
curl_close($ch); //关闭连接
Log::DEBUG("checkAppQrv返回结果为:" .$content);
//返回内容JSON_DECODE
return json_decode($content, true);
} catch (Exception $e) {
Log::DEBUG("检查图片出错:" . $e->getTraceAsString());
return false;
}
}
public static function checkAppQrv_fsockopen($imageFullPath, $fileName, $token)
{
//第二种方法:fsockopen方法上传
// 获取图片内容
$filename_root = $_SERVER['DOCUMENT_ROOT'];
$path = $filename_root. '/tmp/'. date("Ymd");
$saveFilePath = $path.'/'.$fileName;
$arr_data = array('fileName' => $fileName,
'token' => $token); //普通参数
$var_file='img'; //文件变量名
$file_type='image/jpeg'; //文件类型
$filepath = realpath($saveFilePath); //文件路径
$filestring = @file_get_contents($filepath) or exit('not found file ( '.$filepath.' )'); //生成文件流
$host = "check1.bosch-smartlife.com";
$requestPath = self::CHECK_URL;
//构造post请求的头
$boundary = substr(md5(time()),8,16); //分隔符
$header = "POST {$requestPath} HTTP/1.1\r\n";//一般有post, get这两种
$header .= "Host: {$host}\r\n";
$header .= "Content-Type: multipart/form-data; boundary={$boundary}\r\n";
$data = "";
//请求普通数据
foreach($arr_data as $k=>$v){
$data .= "--{$boundary}\r\n";
$data .= "Content-Disposition: form-data; name=\"{$k}\"\r\n";
$data .= "\r\n{$v}\r\n";
$data .= "--{$boundary}\r\n";
}
//请求图片数据
$filename = basename($filepath); //文件名
$data .= "--{$boundary}\r\n";
$data .= "Content-Disposition: form-data; name=\"$var_file\"; filename=\"$filename\"\r\n";
$data .= "Content-Type: $file_type\r\n"; //\r\n不可少
$data .= "\r\n$filestring\r\n"; //\r\n不可少
$data .= "--{$boundary}\r\n"; //\r\n不可少
$header .= "Content-Length: ".strlen($data)."\r\n\r\n"; //\r\n不可少
//发送post的数据
$fp = fsockopen($host,80,$errno,$errstr,10) or exit($errstr."--->".$errno);
fputs($fp,$header.$data);
$inheader = 0; //1去除请求包的头只显示页面的返回数据 0-保留头
while (!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
Log::DEBUG("返回信息:" . $line);
}
}
return "";
}
/**
* 防伪标签的激活
* @param $uuid string 是 防伪标签uuid
* @param $token string 是 访问令牌
* @return mixed|string {"status": "success"}
* 状态码:success:激活成功 fail:激活失败,无效标签 activated:已激活过
*/
public static function getAppActivate($uuid, $token)
{
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::ACTIVE_URL);
/*curl_setopt($curl, CURLOPT_HTTPHEADER, array (
'Authorization:APPCODE '.self::APPCODE_IMAGE
));*/
//设置post数据
$post_data = array(
"uuid" => $uuid,
"token" => $token
);;
//如果是https协议
if (stripos(self::ACTIVE_URL, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
}
//通过POST方式提交
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//超时时间
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//返回内容
$callbcak = curl_exec($curl);
//http状态码
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
//状态处理
if (in_array($httpCode, array(400, 403)))
return "";
//关闭,释放资源
curl_close($curl);
//返回内容JSON_DECODE
return json_decode($callbcak, true);
} catch (Exception $e) {
return false;
}
}
/**
* 校验防伪标签编号
* @param $uuid string 是 防伪标签uuid
* @param $token string 是 访问令牌
* @return mixed|string {"status": "success"}
* 状态码:success:激活成功 fail:激活失败,无效标签 activated:已激活过
*/
public static function getAppCheck($uuid, $token)
{
try {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::APP_CHECK);
/*curl_setopt($curl, CURLOPT_HTTPHEADER, array (
'Authorization:APPCODE '.self::APPCODE_IMAGE
));*/
//设置post数据
$post_data = array(
"uuid" => $uuid,
"token" => $token
);;
//如果是https协议
if (stripos(self::ACTIVE_URL, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
}
//通过POST方式提交
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//超时时间
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//返回内容
$callbcak = curl_exec($curl);
//http状态码
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
//状态处理
if (in_array($httpCode, array(400, 403)))
return "";
//关闭,释放资源
curl_close($curl);
//返回内容JSON_DECODE
return json_decode($callbcak, true);
} catch (Exception $e) {
return false;
}
}
/**
* 将图片转成二进制流
* @param $strTmpName
* @return string
*/
private static function base64EncodeImage($strTmpName)
{
$base64Image = '';
$imageInfo = getimagesize($strTmpName);
$imageData = fread(fopen($strTmpName , 'r'), filesize($strTmpName));
$base64Image = 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode($imageData));
return $base64Image;
}
}