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

作者:俏巴


概述


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


Code Sample


1、环境依赖


  • composer.json

{
<span class="hljs-string">"require"</span>: {
  <span class="hljs-string">"guzzlehttp/guzzle"</span>: <span class="hljs-string">"~6.0"</span>

}
}


2、使用网络图片


<?php

use GuzzleHttpPsr7Request;
use GuzzleHttpClient;
require_once 'vendorautoload.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 GMT");
// 参数构造
$options = array(

<span class="hljs-string">'http'</span> =&gt; array(
    <span class="hljs-string">'header'</span> =&gt; array(
        <span class="hljs-string">'accept'</span>=&gt; <span class="hljs-string">"application/json"</span>,
        <span class="hljs-string">'content-type'</span>=&gt; <span class="hljs-string">"application/json"</span>,
        <span class="hljs-string">'date'</span>=&gt; $date1,
        <span class="hljs-string">'authorization'</span> =&gt; <span class="hljs-string">''</span>
    ),
    <span class="hljs-string">'method'</span> =&gt; <span class="hljs-string">"POST"</span>, <span class="hljs-comment">//可以是 GET, POST, DELETE, PUT</span>
    <span class="hljs-string">'content'</span> =&gt; $body1

// 'content' => json_encode($body1)

)

);

$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))

$path = $urlObj[<span class="hljs-string">"path"</span>];
<span class="hljs-keyword">else</span>
    $path = $urlObj[<span class="hljs-string">"path"</span>].<span class="hljs-string">"?"</span>.$urlObj[<span class="hljs-string">"query"</span>];
    $body = $http[<span class="hljs-string">'content'</span>];
    <span class="hljs-keyword">if</span>(empty($body))
        $bodymd5 = $body;
        <span class="hljs-keyword">else</span>
            $bodymd5 = base64_encode(md5($body,<span class="hljs-literal">true</span>));
            $stringToSign = $http[<span class="hljs-string">'method'</span>].<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'accept'</span>].<span class="hljs-string">"\n"</span>.$bodymd5.<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'content-type'</span>].<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'date'</span>].<span class="hljs-string">"\n"</span>.$path;
            $signature = base64_encode(
                hash_hmac(
                    <span class="hljs-string">"sha1"</span>,
                    $stringToSign,
                    $akSecret, <span class="hljs-literal">true</span>));

$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'['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 GuzzleHttpPsr7Request;
use GuzzleHttpClient;
require_once 'vendorautoload.php';

// 参数设置
$akId = "";
$akSecret = "";

读取本地图片

$image_file = 'C:UsersAdministratorDesktoptime.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(<span class="hljs-string">" "</span>,<span class="hljs-string">" "</span>,<span class="hljs-string">"\t"</span>,<span class="hljs-string">"\n"</span>,<span class="hljs-string">"\r"</span>);
$hou=array(<span class="hljs-string">""</span>,<span class="hljs-string">""</span>,<span class="hljs-string">""</span>,<span class="hljs-string">""</span>,<span class="hljs-string">""</span>);
<span class="hljs-keyword">return</span> 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 GMT");
// 参数构造
$options = array(

<span class="hljs-string">'http'</span> =&gt; array(
    <span class="hljs-string">'header'</span> =&gt; array(
        <span class="hljs-string">'accept'</span>=&gt; <span class="hljs-string">"application/json"</span>,
        <span class="hljs-string">'content-type'</span>=&gt; <span class="hljs-string">"application/json"</span>,
        <span class="hljs-string">'date'</span>=&gt; $date1,
        <span class="hljs-string">'authorization'</span> =&gt; <span class="hljs-string">''</span>
    ),
    <span class="hljs-string">'method'</span> =&gt; <span class="hljs-string">"POST"</span>, <span class="hljs-comment">//可以是 GET, POST, DELETE, PUT</span>
    <span class="hljs-string">'content'</span> =&gt; $body1
)

);

$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))

$path = $urlObj[<span class="hljs-string">"path"</span>];
<span class="hljs-keyword">else</span>
    $path = $urlObj[<span class="hljs-string">"path"</span>].<span class="hljs-string">"?"</span>.$urlObj[<span class="hljs-string">"query"</span>];
    $body = $http[<span class="hljs-string">'content'</span>];
    <span class="hljs-keyword">if</span>(empty($body))
        $bodymd5 = $body;
        <span class="hljs-keyword">else</span>
            $bodymd5 = base64_encode(md5($body,<span class="hljs-literal">true</span>));
            $stringToSign = $http[<span class="hljs-string">'method'</span>].<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'accept'</span>].<span class="hljs-string">"\n"</span>.$bodymd5.<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'content-type'</span>].<span class="hljs-string">"\n"</span>.$header[<span class="hljs-string">'date'</span>].<span class="hljs-string">"\n"</span>.$path;
            $signature = base64_encode(
                hash_hmac(
                    <span class="hljs-string">"sha1"</span>,
                    $stringToSign,
                    $akSecret, <span class="hljs-literal">true</span>));

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


测试结果


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


SSL错误处理


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

上一篇:LAMP网站性能优化记


下一篇:阿里云人脸识别NodeJS调用示例参考