-
Notifications
You must be signed in to change notification settings - Fork 39
/
AI.php
111 lines (107 loc) · 3.29 KB
/
AI.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
class AI
{
protected $appkey="";
protected $appid = "";
/**
* Describe:获取图片信息
* @Author: Bygones
* Date: 2019-06-15
* Time: 23:05
* @param string $img
* @return array
*/
public function get(string $img):array
{
$data = file_get_contents($img);
$img = base64_encode($data);
$params = [
'app_id' => $this->appid,
'image' => $img,
'mode' => '1',
'time_stamp' => strval(time()),
'nonce_str' => strval(rand()),
'sign' => '',
];
$params['sign'] = $this->getReqSign((array) $params, (string) $this->appkey);
$url = 'https://api.ai.qq.com/fcgi-bin/face/face_detectface';
$response = $this->doHttpPost((string) $url, (array) $params);
$response = json_decode($response,true);
if($response["ret"] === 0){
$response["data"] = [
"gender"=>$response["data"]["face_list"][0]["gender"],
"age"=>$response["data"]["face_list"][0]["age"],
"beauty"=>$response["data"]["face_list"][0]["beauty"],
];
return ["code"=>1,"data"=>$response["data"],"message"=>"获取成功"];
}
return ["code"=>0,"data"=>$response["data"],"message"=>"未识别"];
}
/**
* Describe:鉴权签名
* @Author: Bygones
* Date: 2019-06-15
* Time: 22:33
* @param array $params
* @param string $appkey
* @return string
*/
public function getReqSign(array $params,string $appkey):string
{
ksort($params);
$str = '';
foreach ($params as $key => $value){
if ($value !== '')
{
$str .= $key . '=' . urlencode($value) . '&';
}
}
$str .= 'app_key=' . $appkey;
$sign = strtoupper(md5($str));
return $sign;
}
/**
* Describe:Post请求API
* @Author: Bygones
* Date: 2019-06-15
* Time: 22:34
* @param string $url
* @param array $params
* @return bool|string
*/
public function doHttpPost(string $url,array $params):string
{
$curl = curl_init();
$response = false;
do
{
curl_setopt($curl, CURLOPT_URL, $url);
$head = [
'Content-Type: application/x-www-form-urlencoded'
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
$body = http_build_query($params);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if ($response === false)
{
$response = false;
break;
}
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 200)
{
$response = false;
break;
}
} while (0);
curl_close($curl);
return $response;
}
}