PHP SDK调用示例
本文介绍了增强版活体检测方案的PHP SDK调用示例。
SDK使用说明
如果需要其他语言的SDK示例代码,您可以通过OpenAPI开发者门户在线调试工具调试API接口。该工具会自动生成相应API的SDK调用示例代码,详细信息,请参见:
前提条件
需要使用的PHP版本在5.6版本及以上。
已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景。
安装SDK
关于PHP SDK源代码,请参见PHP SDK包。
通过Composer来管理项目依赖(推荐)
composer require alibabacloud/cloudauth-20200618 1.0.5
使用composer.json文件
{ "require": { "alibabacloud/cloudauth-20200618": "1.0.5" } }
如果因网络问题导致无法按照上述操作安装SDK,您可以使用阿里云Composer全量镜像。
调用示例
关于参数的具体描述,请参见InitSmartVerify、DescribeSmartVerify。
InitSmartVerify接口调用示例
<?php
namespace AlibabaCloud\SDK\Sample {
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\SDK\Cloudauth\V20200618\Cloudauth;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Rpc\Rpc\Config;
use AlibabaCloud\SDK\Cloudauth\V20200618\Models\InitSmartVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20200618\Models\InitSmartVerifyResponse;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class InitSmartVerify {
/**
* @param string $endpoint
* @return Cloudauth
*/
public static function createClient($endpoint){
// 阿里云账号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 InitSmartVerifyRequest([
// 请输入场景ID。
"sceneId" => <xxx>,
"outerOrderNo" => "<商户请求的唯一标识>",
// 证件类型,固定值。
"mode" => "ACCOUNT_SAFE",
"certType" => "IDENTITY_CARD",
// "certName" => "<张三>",
// "certNo" => "<xxxxx>",
// MetaInfo环境参数,需要通过客户端SDK获取。
"metaInfo" => '{"zimVer":"3.0.0","appVersion": "1","bioMetaInfo": "4.1.0:11501568,0",
"appName": "com.aliyun.cloudauth","deviceType": "ios","osVersion": "iOS 10.3.2","apdidToken": "","deviceModel": "iPhone9,1"}',
"mobile" => "xxx",
//"ip" => "<114.xxx.xxx.xxx>",
//"userId" => "<12345xxxx>",
//"callbackUrl" => "<https://www.aliyundoc.com>",
//"callbackToken" => "<xxxxxxx>"
]);
// 推荐,支持服务路由。
$response = self::initSmartVerifyAutoRoute($request);
// 不支持服务自动路由。
// $response = self::initSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", $request);
var_dump($response->requestId);
var_dump($response->code);
var_dump($response->message);
var_dump($response->resultObject->certifyId);
}
/**
* @param InitSmartVerifyRequest $request
* @return InitSmartVerifyResponse
*/
public static function initSmartVerifyAutoRoute(InitSmartVerifyRequest $request){
$endpoints = [
"cloudauth.cn-shanghai.aliyuncs.com",
"cloudauth.cn-beijing.aliyuncs.com"
];
foreach($endpoints as $endpoint){
try {
$response = self::initSmartVerify($endpoint, $request);
if (Utils::equalString("500", $response->code)) {
continue;
}
return $response;
}
catch (Exception $err) {
if (!($err instanceof TeaError)) {
$err = new TeaError([], $err->getMessage(), $err->getCode(), $err);
}
// 网络异常,切换到下个区域调用。
if (Utils::equalString("ServiceUnavailable", $err->code)) {
continue;
}
}
}
return null;
}
/**
* @param string $endpoint
* @param InitSmartVerifyRequest $request
* @return InitSmartVerifyResponse
*/
public static function initSmartVerify($endpoint, $request){
$client = self::createClient($endpoint);
// 创建RuntimeObject实例并设置运行参数。
$runtime = new RuntimeOptions([]);
$runtime->readTimeout = 10000;
$runtime->connectTimeout = 10000;
return $client->initSmartVerify($request, $runtime);
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
InitSmartVerify::main();
}
DescribeSmartVerify接口调用示例
<?php
namespace AlibabaCloud\SDK\Sample {
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\SDK\Cloudauth\V20200618\Cloudauth;
use AlibabaCloud\SDK\Cloudauth\V20200618\Models\DescribeSmartVerifyRequest;
use AlibabaCloud\SDK\Cloudauth\V20200618\Models\DescribeSmartVerifyResponse;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Rpc\Rpc\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class DescribeSmartVerify {
/**
* @param string $endpoint
* @return Cloudauth
*/
public static function createClient($endpoint){
// 阿里云账号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 DescribeSmartVerifyRequest([
// 请输入场景ID。
"sceneId" => <xxxx>,
"certifyId" => "<xxxx>"
]);
// 推荐,支持服务路由。
$response = self::describeSmartVerifyAutoRoute($request);
// 不支持服务自动路由。
// $response = self::describeSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", $request);
var_dump($response->requestId);
var_dump($response->code);
var_dump($response->message);
var_dump($response->resultObject->passed);
var_dump($response->resultObject->subCode);
var_dump($response->resultObject->materialInfo);
var_dump($response->resultObject->passedScore);
}
/**
* @param DescribeSmartVerifyRequest $request
* @return DescribeSmartVerifyResponse
*/
public static function describeSmartVerifyAutoRoute(DescribeSmartVerifyRequest $request){
$endpoints = [
"cloudauth.cn-shanghai.aliyuncs.com",
"cloudauth.cn-beijing.aliyuncs.com"
];
foreach($endpoints as $endpoint){
try {
$response = self::describeSmartVerify($endpoint, $request);
if (Utils::equalString("500", $response->code)) {
continue;
}
return $response;
}
catch (Exception $err) {
if (!($err instanceof TeaError)) {
$err = new TeaError([], $err->getMessage(), $err->getCode(), $err);
}
// 网络异常,切换到下个区域调用。
if (Utils::equalString("ServiceUnavailable", $err->code)) {
continue;
}
}
}
return null;
}
/**
* @param string $endpoint
* @param DescribeSmartVerifyRequest $request
* @return DescribeSmartVerifyResponse
*/
public static function describeSmartVerify($endpoint, $request){
$client = self::createClient($endpoint);
// 创建RuntimeObject实例并设置运行参数。
$runtime = new RuntimeOptions([]);
$runtime->readTimeout = 10000;
$runtime->connectTimeout = 10000;
return $client->describeSmartVerify($request, $runtime);
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
DescribeSmartVerify::main();
}