PHP SDK
本章节介绍新版PHP SDK,您可以参考本章节进行快速集成开发。
准备工作
- 注册华为账号并开通华为云,并完成实名认证,账号不能处于欠费或冻结状态。
- 已具备开发环境,PHP 5.6 及以上版本,可执行 php --version 检查当前的版本信息。
- 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
图1 获取AK、SK
- 登录“我的凭证”页面,获取“IAM用户名”、“账号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。
本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。
图2 我的凭证
安装SDK
推荐使用 Composer 安装 SDK 。
Composer 是 php 的依赖管理工具,允许您在项目中声明依赖关系并安装这些依赖:
// 安装 Composer curl -sS https://getcomposer.org/installer | php // 安装 PHP SDK composer require huaweicloud/huaweicloud-sdk-php
安装完毕后,你需要引入 Composer 的自动加载文件:
require 'path/to/vendor/autoload.php';
开始使用
在开始使用之前,请确保您安装的是最新版本的SDK。使用过时的版本可能会导致兼容性问题或无法使用最新功能。您可以通过运行以下命令来检查并更新SDK至最新版本。
composer show huaweicloud/huaweicloud-sdk-php composer update huaweicloud/huaweicloud-sdk-php
详细的SDK介绍,使用异步客户端,配置日志请参见SDK中心、PHP SDK使用指导、PHP SDK使用视频。
- 导入依赖模块
<?php namespace HuaweiCloud\SDK\Frs\V2\Model; require_once "vendor/autoload.php"; use HuaweiCloud\SDK\Core\Auth\BasicCredentials; use HuaweiCloud\SDK\Core\Http\HttpConfig; use HuaweiCloud\SDK\Core\Exceptions\ConnectionException; use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException; use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException; use HuaweiCloud\SDK\Frs\V2\FrsClient;
- 配置客户端连接参数
- 默认配置
// 使用默认配置 $config = HttpConfig::getDefaultConfig();
- 网络代理(可选)
// 使用代理服务器 $config->setProxyProtocol('http'); $config->setProxyHost('proxy.huawei.com'); $config->setProxyPort(8080); $config->setProxyUser('username'); $config->setProxyPassword('password');
- 超时配置(可选)
// 默认连接超时时间为60秒,读取超时时间为120秒。可根据需要修改默认值。 $config->setTimeout(120); $config->setConnectionTimeout(60);
- SSL配置(可选)
// 配置跳过服务端证书验证 $config->setIgnoreSslVerification(true); // 配置服务器端CA证书,用于SDK验证服务端证书合法性 $config->setCertFile("{yourCertFile}");
- 默认配置
- 配置认证信息
配置AK、SK、projectId信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。
// 终端节点以 FRS 服务北京四的 endpoint 为例 $ak = getenv('HUAWEICLOUD_SDK_AK'); $sk = getenv('HUAWEICLOUD_SDK_SK'); $endpoint = "https://face.cn-north-4.myhuaweicloud.com"; $projectId = getenv('PROJECT_ID'); $credentials = new BasicCredentials($ak,$sk,$projectId);
认证参数说明:
- ak、sk:访问秘钥信息,获取方法请参见准备工作。
- projectId:华为云项目ID,获取方法请参见准备工作。
- endpoint:华为云各服务应用区域和各服务的终端节点,详情请查看 地区和终端节点 。
- 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。
- 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK,HUAWEICLOUD_SDK_SK和PROJECT_ID。
图3 Windows环境新建环境变量
- 初始化客户端
$client = FrsClient::newBuilder(new FrsClient) ->withHttpConfig($config) ->withEndpoint($endpoint) ->withCredentials($credentials) ->build();
- 发送并查看响应
// 以调用人脸检测接口 DetectFaceByBase64 为例 $request = new DetectFaceByBase64Request(); $body = new FaceDetectBase64Req(); $body->setImageBase64("图片的base64编码"); $request->setBody($body); try { $response = $client->DetectFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。
- 异常处理
表1 异常处理 一级分类
一级分类说明
二级分类
二级分类说明
ConnectionException
连接类异常
HostUnreachableException
网络不可达、被拒绝。
SslHandShakeException
SSL认证异常。
RequestTimeoutException
响应超时异常
CallTimeoutException
单次请求,服务器处理超时未返回。
RetryOutageException
在重试策略消耗完成后,仍无有效的响应。
ServiceResponseException
服务器响应异常
ServerResponseException
服务端内部错误,Http响应码:[500,]。
ClientRequestException
请求参数不合法,Http响应码:[400, 500)
// 捕获和处理不同类型的异常 try { $response = $client->DetectFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
SDK demo 代码解析
- 人脸检测
$request = new DetectFaceByBase64Request(); $body = new FaceDetectBase64Req(); $body->setImageBase64("图片的base64编码"); $request->setBody($body); try { $response = $client->DetectFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 人脸比对
$request = new CompareFaceByBase64Request(); $body = new FaceCompareBase64Req(); $body->setImage1Base64("图片1的base64编码"); $body->setImage2Base64("图片2的base64编码"); $request->setBody($body); try { $response = $client->CompareFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 人脸搜索
$request = new SearchFaceByBase64Request(); $body = new FaceSearchBase64Req(); $body->setImageBase64("图片的base64编码"); $request->setBody($body); try { $response = $client->SearchFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 创建人脸库
$request = new CreateFaceSetRequest(); $body = new CreateFaceSetReq(); $body->setFaceSetName("人脸库名称"); $request->setBody($body); try { $response = $client->CreateFaceSet($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 查询人脸库
$request = new ShowFaceSetRequest(); $request->setFaceSetName("人脸库名称"); try { $response = $client->ShowFaceSet($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 查询所有人脸库
$request = new ShowAllFaceSetsRequest(); try { $response = $client->ShowAllFaceSets($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 删除人脸库
$request = new DeleteFaceSetRequest(); $request->setFaceSetName("人脸库名称"); try { $response = $client->DeleteFaceSet($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 添加人脸
$request = new AddFacesByBase64Request(); $body = new AddFacesBase64Req(); $body->setImageBase64("图片的base64编码"); $request->setBody($body); try { $response = $client->AddFacesByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 删除人脸
$request = new DeleteFaceByFaceIdRequest(); $request->setFaceSetName("人脸库名称"); $request->setFaceId("人脸ID"); try { $response = $client->DeleteFaceByFaceId($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 批量删除人脸
$request = new BatchDeleteFacesRequest(); $request->setFaceSetName("人脸库名称"); $body = newDeleteFacesBatchReq(); $body->setFilter("过滤条件"); $request->setBody($body); try { $response = $client->BatchDeleteFaces($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 更新人脸
$request = new UpdateFaceRequest(); $request->setFaceSetName("人脸库名称"); $body = newUpdateFaceReq(); $body->setFaceId("人脸库ID"); $request->setBody($body); try { $response = $client->UpdateFace($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 查询人脸
$request = newShowFacesByFaceIdRequest(); $request->setFaceSetName("人脸库名称"); $request->setFaceId("人脸ID"); try { $response = $client->ShowFacesByFaceId($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 动作活体检测
$request = new DetectLiveByBase64Request(); $body = new LiveDetectBase64Req(); $body->setActions("动作代码顺序列表"); $body->setVideoBase64("视频数据的base64编码"); $request->setBody($body); try { $response = $client->DetectLiveByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
- 静默活体检测
$request = new DetectLiveFaceByBase64Request(); $body = new LiveDetectFaceBase64Req(); $body->setImageBase64("图片的base64编码"); $request->setBody($body); try { $response = $client->DetectLiveFaceByBase64($request); } catch (ConnectionException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (RequestTimeoutException $e) { $msg = $e->getMessage(); echo "\n". $msg ."\n"; } catch (ServiceResponseException $e) { echo "\n"; echo $e->getHttpStatusCode(). "\n"; echo $e->getErrorCode() . "\n"; echo $e->getErrorMsg() . "\n"; } echo "\n"; echo $response;
SDK代码自动生成
API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查看和在线咨询。
您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。