本文介绍人脸静默活体检测API的PHP SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。您可以单击以下接口链接进行调试:

调试LivenessFaceVerify接口

前提条件

  • 仅支持PHP 5.6及以上版本。
  • 已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景

安装SDK

关于PHP SDK源代码,请参见PHP SDK包

  • 通过Composer来管理项目依赖(推荐)
    composer require alibabacloud/cloudauth-20190307 2.0.4
  • 使用composer.json文件
    {
       "require": {
           "alibabacloud/cloudauth-20190307": "2.0.4"
        }
    }

如果因网络问题导致无法按照上述操作安装SDK,您可以使用阿里云Composer全量镜像

调用示例

LivenessFaceVerify接口调用示例

<?php


namespace AlibabaCloud\SDK\Sample\Verify\LRFR;


use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\LivenessFaceVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\LivenessFaceVerifyResponse;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Exception;

class LivenessFaceVerify
{
    /**
     * createClient
     * @param string $endpoint
     * @return Cloudauth
     */
    public static function createClient(string $endpoint): Cloudauth
    {
        $config = new Config([
            // 您的AccessKey ID。
            "accessKeyId" => "<您的AccessKey ID>",
            // 您的AccessKey Secret。
            "accessKeySecret" => "<您的AccessKey Secret>",
            // 设置HTTP代理。
            // "httpProxy" => "<http://xx.xx.xx.xx:xxxx>",
            // 设置HTTPS代理。
            // "httpsProxy" => "<https://xx.xx.xx.xx:xxxx>",
            "endpoint" => $endpoint

        ]);
        return new Cloudauth($config);
    }

    /**
     * @return void
     */
    public static function main(){
        $request = new LivenessFaceVerifyRequest([
            // 请输入场景ID。
            "sceneId" => 0,
            "outerOrderNo" => "<商户请求的唯一标识>",
            // 要接入的认证方案,固定值:LR_FR_MIN。
            "productCode" => "<LR_FR_MIN>",
            // 活体检测类型。
            "model" => "<FRONT_CAMERA_LIVENESS>",
            // 设备Token,用于风险识别。
            // "deviceToken" => "<McozS1ZWRcRZStlERcZZo_QOytx5jcgZoZJEoRLOxxxxxxx>",
            // "mobile" => "<LR_FR_MIN>",
            // "ip" => "<114.xxx.xxx.xxx>",
            // "userId" => "<123456789>",
            // 请在以下方式中选择一种,传入人脸图片。
            // 方式一:人脸图片Base64。
            // "faceContrastPicture" => "<人脸图片Base64编码>",
            // 方式二:人脸图片OSS Bucket名和文件名。
            // "ossBucketName" => "<cn-shanghai-aliyun-cloudauth-xxxxx>",
            // "ossObjectName" => "<verify/xxxxx/xxxxxx.jpeg>",
            // 方式三:人脸图片的OSS URL地址。该地址必须公网可访问。
            "faceContrastPictureUrl" => "<https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg>"
        ]);
        // 推荐,支持服务路由。
        $response = self::livenessFaceVerifyAutoRoute($request);
        // 不支持服务自动路由。
        // $response = self::livenessFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", $request);
        var_dump($response->body->requestId);
        var_dump($response->body->code);
        var_dump($response->body->message);
        var_dump($response->body->resultObject->materialInfo);
        var_dump($response->body->resultObject->subCode);
        var_dump($response->body->resultObject->passed);
    }

    /**
     * livenessFaceVerifyAutoRoute
     * @param LivenessFaceVerifyRequest $request
     * @return LivenessFaceVerifyResponse|null
     */
    public static function livenessFaceVerifyAutoRoute(LivenessFaceVerifyRequest $request): ?LivenessFaceVerifyResponse
    {
        $endpoints = [
            "cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com"
        ];
        foreach($endpoints as $endpoint){
            try {
                $response = self::livenessFaceVerify($endpoint, $request);
                if (Utils::equalNumber(500, $response->statusCode)) {
                    continue;
                }
                if(Utils::equalString("500", $response->body->code)){
                    continue;
                }
                return $response;
            }
            catch (Exception $err) {
                var_dump($err -> getCode());
                var_dump($err -> getMessage());
            }
        }
        return null;
    }

    /**
     * livenessFaceVerify
     * @param string $endpoint
     * @param LivenessFaceVerifyRequest $request
     * @return LivenessFaceVerifyResponse
     */
    public static function livenessFaceVerify(string $endpoint, LivenessFaceVerifyRequest $request): LivenessFaceVerifyResponse
    {
        $client = self::createClient($endpoint);
        // 创建RuntimeObject实例并设置运行参数。
        $runtime = new RuntimeOptions([]);
        $runtime->readTimeout = 10000;
        $runtime->connectTimeout = 10000;
        return $client->livenessFaceVerifyWithOptions($request, $runtime);
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
LivenessFaceVerify::main();