35 Star 266 Fork 75

Bygones / PHP-DouyinRobot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
AI.php 3.29 KB
一键复制 编辑 原始数据 按行查看 历史
Bygones 提交于 2021-11-21 13:26 . update AI.php.
<?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;
}
}
PHP
1
https://gitee.com/Bygones/PHP-DouyinRobot.git
git@gitee.com:Bygones/PHP-DouyinRobot.git
Bygones
PHP-DouyinRobot
PHP-DouyinRobot
master

搜索帮助