阿里云人脸识别PHP调用示例参考-阿里云开发者社区

开发者社区> 开发与运维> 正文

阿里云人脸识别PHP调用示例参考

简介: 前面分别给出了关于阿里云人脸识别Java调用示例参考、阿里云人脸识别C#调用示例参考、阿里云人脸识别Python3调用示例参考 。本文主要介绍PHP调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介。

概述

前面分别给出了关于阿里云人脸识别Java调用示例参考阿里云人脸识别C#调用示例参考阿里云人脸识别Python3调用示例参考 。本文主要介绍PHP调用阿里云人脸识别服务,参数等的获取参考阿里云人脸识别使用流程简介

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();
?>

测试结果

_

SSL错误处理

PHP 调用认知服务证书认证问题

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章