Code Sample
1、环境依赖
- composer.json
{
"require": {
"guzzlehttp/guzzle": "~6.0"
}
}
2、使用网络图片
<?php
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;
require_once 'vendor\autoload.php';
// 参数设置
$akId = "********";
$akSecret = "********";
$body1 = '{"type":"0","image_url":"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1553926699&di=3e4484731c8897c57e67b3f632801f9a&src=http://b-ssl.duitang.com/uploads/item/201603/28/20160328121906_ErzAB.jpeg"}';
$url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";
$date1 = gmdate("D, d M Y H:i:s \G\M\T");
// 参数构造
$options = array(
'http' => array(
'header' => array(
'accept'=> "application/json",
'content-type'=> "application/json",
'date'=> $date1,
'authorization' => ''
),
'method' => "POST", //可以是 GET, POST, DELETE, PUT
'content' => $body1
// 'content' => json_encode($body1)
)
);
$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))
$path = $urlObj["path"];
else
$path = $urlObj["path"]."?".$urlObj["query"];
$body = $http['content'];
if(empty($body))
$bodymd5 = $body;
else
$bodymd5 = base64_encode(md5($body,true));
$stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
$signature = base64_encode(
hash_hmac(
"sha1",
$stringToSign,
$akSecret, true));
$authHeader = "Dataplus "."$akId".":"."$signature";
$options['http']['header']['authorization'] = $authHeader;
# 构造Rest API Client请求
$client = new Client();
$headers = ['Content-type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => $options['http']['header']['authorization'], 'Date' => $date1];
$request = new Request('POST','https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute',$headers,$body1);
$response = $client->send($request);
echo $response->getBody();
?>
3、使用本地图片
<?php
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;
require_once 'vendor\autoload.php';
// 参数设置
$akId = "********";
$akSecret = "********";
# 读取本地图片
$image_file = 'C:\Users\Administrator\Desktop\time.jpeg';
$image_info = getimagesize($image_file);
$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
function trimall($str)//删除所有的空格
{
$qian=array(" "," ","\t","\n","\r");
$hou=array("","","","","");
return str_replace($qian,$hou,$str);
}
$base64_image_content = substr($base64_image_content, 23);
$base64_image_content = trimall($base64_image_content);
// echo $base64_image_content;
$body1 = '{"type":"1","content":"'.$base64_image_content.'"}';
$url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";
$date1 = gmdate("D, d M Y H:i:s \G\M\T");
// 参数构造
$options = array(
'http' => array(
'header' => array(
'accept'=> "application/json",
'content-type'=> "application/json",
'date'=> $date1,
'authorization' => ''
),
'method' => "POST", //可以是 GET, POST, DELETE, PUT
'content' => $body1
)
);
$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))
$path = $urlObj["path"];
else
$path = $urlObj["path"]."?".$urlObj["query"];
$body = $http['content'];
if(empty($body))
$bodymd5 = $body;
else
$bodymd5 = base64_encode(md5($body,true));
$stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
$signature = base64_encode(
hash_hmac(
"sha1",
$stringToSign,
$akSecret, true));
$authHeader = "Dataplus "."$akId".":"."$signature";
$options['http']['header']['authorization'] = $authHeader;
# 构造Rest API Client请求
$client = new Client();
$headers = ['Content-type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => $options['http']['header']['authorization'], 'Date' => $date1];
$request = new Request('POST','https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute',$headers,$body);
$response = $client->send($request);
echo $response->getBody();
?>