PHP SDK调用示例

本文介绍了金融级实人认证纯服务端API的PHP SDK调用示例。

SDK使用说明

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

调试ContrastFaceVerify接口

前提条件

  • 仅支持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全量镜像

照片实人认证调用示例

关于参数的具体描述,请参见照片实人认证

<?php


namespace AlibabaCloud\SDK\Sample\Verify\Id;

use AlibabaCloud\Credentials\Credential; 
use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyResponse;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Exception;

class ContrastFaceVerifyWithPicture
{
    /**
     * createClient
     * @param string $endpoint
     * @return Cloudauth
     */
    public static function createClient(string $endpoint): Cloudauth
    {
        // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        // 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.com/document_detail/311677.html。 
        $credential = new Credential([]);
        $config = new Config([
			  // 使用credential配置凭证
    		    "credential" => $credential,
            // 设置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 ContrastFaceVerifyRequest([
            // 请输入场景ID。
            "sceneId" => 0,
            "outerOrderNo" => "<商户请求的唯一标识>",
            // 要接入的认证方案。
            "productCode" => "<ID_MIN>",
            // 要进行活体检测的类型。
            "model" => "<NO_LIVENESS>",
            "certType" => "<IDENTITY_CARD>",
            "certName" => "<xxx>",
            "certNo" => "<xxx>",
            // "deviceToken" => "<McozS1ZWRcRZStlERcZZo_QOytx5jcgZoZJEoRLOxxxxxxx>",
            // "mobile" => "<xxx>",
            // "ip" => "<114.xxx.xxx.xxx>",
            // "userId" => "<12345xxxx>",
            // 请在以下四种方式中选择其一。
            // 1.照片Base64编码。
            // 2.照片OSS Bucket名和文件名。
            // 3.照片OSS的URL地址。
            // 4.已经实人认证通过的CertifyId。

            // 方式一:照片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>",
            // 方式四:已经实人认证通过的CertifyId。
            // "certifyId" => "<0bfa7c493f850e5178b9f8613634c9xx>",

            // 如需开启个人信息加密传输。
            // "encryptType" => "<SM2>",
            // "certName" => "<BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==>",
            // "certNo" => "<BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg==>"
        ]);
        // 推荐,支持服务路由。
        $response = self::contrastFaceVerifyAutoRoute($request);
        // 不支持服务自动路由。
        // $response = self::contrastFaceVerify("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->identityInfo);
        var_dump($response->body->resultObject->passed);
    }

    /**
     * contrastFaceVerifyAutoRoute
     * @param ContrastFaceVerifyRequest $request
     * @return ContrastFaceVerifyResponse|null
     */
    public static function contrastFaceVerifyAutoRoute(ContrastFaceVerifyRequest $request): ?ContrastFaceVerifyResponse
    {
        $endpoints = [
            "cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com"
        ];
        foreach($endpoints as $endpoint){
            try {
                $response = self::contrastFaceVerify($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;
    }

    /**
     * contrastFaceVerify
     * @param string $endpoint
     * @param ContrastFaceVerifyRequest $request
     * @return ContrastFaceVerifyResponse
     */
    public static function contrastFaceVerify(string $endpoint, ContrastFaceVerifyRequest $request): ContrastFaceVerifyResponse
    {
        $client = self::createClient($endpoint);
        // 创建RuntimeObject实例并设置运行参数。
        $runtime = new RuntimeOptions([]);
        $runtime->readTimeout = 10000;
        $runtime->connectTimeout = 10000;
        return $client->contrastFaceVerifyWithOptions($request, $runtime);
    }
}

$path = __DIR__ . \DIRECTORY_SEPARATOR . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
ContrastFaceVerifyWithPicture::main();

视频实人认证调用示例

支持OSS对象存储方式和本地文件两种方式。关于参数的具体描述,请参见视频实人认证

使用本地文件的方式(推荐)

<?php


namespace AlibabaCloud\SDK\Sample\Verify\Id;

use AlibabaCloud\Credentials\Credential; 
use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyAdvanceRequest;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyResponse;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

class ContrastFaceVerifyWithVideoFile
{
    /**
     * createClient
     * @param string $endpoint
     * @return Cloudauth
     */
    public static function createClient(string $endpoint): Cloudauth
    {
        // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        // 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.com/document_detail/311677.html。 
        $credential = new Credential([]);
        $config = new Config([
			  // 使用credential配置凭证
    		"credential" => $credential,
            // 设置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(){
        $file = new Stream(fopen('本地文件路径', 'r'));
        $request = new ContrastFaceVerifyAdvanceRequest([
            // 请输入场景ID。
            "sceneId" => 0,
            "outerOrderNo" => "<商户请求的唯一标识>",
            // 要接入的认证方案。
            "productCode" => "<ID_MIN_VIDEO>",
            // 要进行活体检测的类型。
            "model" => "<FRONT_CAMERA_LIVENESS>",
            "certType" => "<IDENTITY_CARD>",
            "certName" => "<xxx>",
            "certNo" => "<xxx>",
            // "deviceToken" => "<McozS1ZWRcRZStlERcZZo_QOytx5jcgZoZJEoRLOxxxxxxx>",
            // "mobile" => "<xxx>",
            // "ip" => "<114.xxx.xxx.xxx>",
            // "userId" => "<12345xxxx>",
            "faceContrastFileObject" => $file,
            // 如需开启个人信息加密传输。
            // "encryptType" => "<SM2>",
            // "certName" => "<BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==>",
            // "certNo" => "<BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg==>"
        ]);
        try {
            // 推荐,支持服务路由。
             $response = self::contrastFaceVerifyAutoRoute($request);
            // 不支持服务自动路由。
            // $response = self::contrastFaceVerify("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->identityInfo);
            var_dump($response->body->resultObject->passed);
        } finally {
            fclose($file);
        }
    }

    /**
     * contrastFaceVerifyAutoRoute
     * @param ContrastFaceVerifyAdvanceRequest $request
     * @return ContrastFaceVerifyResponse|null
     */
    public static function contrastFaceVerifyAutoRoute(ContrastFaceVerifyAdvanceRequest $request): ?ContrastFaceVerifyResponse
    {
        $endpoints = [
            "cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com"
        ];
        foreach($endpoints as $endpoint){
            try {
                $response = self::contrastFaceVerify($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;
    }

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

使用OSS对象存储的方式

<?php


namespace AlibabaCloud\SDK\Sample\Verify\Id;

use AlibabaCloud\Credentials\Credential; 
use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyResponse;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class ContrastFaceVerifyWithVideoOSS
{
    /**
     * createClient
     * @param string $endpoint
     * @return Cloudauth
     */
    public static function createClient(string $endpoint): Cloudauth
    {
        // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        // 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见 https://help.aliyun.com/document_detail/311677.html。 
        $credential = new Credential([]);
        $config = new Config([
		    // 使用credential配置凭证
    		"credential" => $credential,
            // 设置HTTP代理。
            // "httpProxy" => "<http://xx.xx.xx.xx:xxxx>",
            // 设置HTTPS代理
            // "httpsProxy" => "<https://xx.xx.xx.xx:xxxx>",
            "endpoint" => $endpoint

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

    /**
     * main
     * @return void
     */
    public static function main(){
        $request = new ContrastFaceVerifyRequest([
            // 请输入场景ID。
            "sceneId" => 0,
            "outerOrderNo" => "<商户请求的唯一标识>",
            // 要接入的认证方案。
            "productCode" => "<ID_MIN_VIDEO>",
            // 要进行活体检测的类型。
            "model" => "<FRONT_CAMERA_LIVENESS>",
            "certType" => "<IDENTITY_CARD>",
            "certName" => "<xxx>",
            "certNo" => "<xxx>",
            // "deviceToken" => "<McozS1ZWRcRZStlERcZZo_QOytx5jcgZoZJEoRLOxxxxxxx>",
            // "mobile" => "<xxx>",
            // "ip" => "<114.xxx.xxx.xxx>",
            // "userId" => "<12345xxxx>",
            "ossBucketName" => "<cn-shanghai-aliyun-cloudauth-xxxxx>",
            "ossObjectName" => "<verify/xxxxx/xxxxxx.mp4>",
            // 如需开启个人信息加密传输。
            // "encryptType" => "<SM2>",
            // "certName" => "<BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==>",
            // "certNo" => "<BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg==>"
        ]);
        // 推荐,支持服务路由。
        $response = self::contrastFaceVerifyAutoRoute($request);
        // 不支持服务自动路由。
        // $response = self::contrastFaceVerify("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->identityInfo);
        var_dump($response->body->resultObject->passed);
    }

    /**
     * contrastFaceVerifyAutoRoute
     * @param ContrastFaceVerifyRequest $request
     * @return ContrastFaceVerifyResponse|null
     */
    public static function contrastFaceVerifyAutoRoute(ContrastFaceVerifyRequest $request): ?ContrastFaceVerifyResponse
    {
        $endpoints = [
            "cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com"
        ];
        foreach($endpoints as $endpoint){
            try {
                $response = self::contrastFaceVerify($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;
    }

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