阿里云API调试工具OpenAPI Explore提供OpenAPI文档、API调试和SDK示例,帮助您快速入门API开发。本文介绍如何安装和快速使用智能媒体管理PHP SDK。
前提条件
已创建并获取AccessKey。具体操作,请参见创建AccessKey。
已开通OSS服务、创建存储空间并上传文件到存储空间。具体操作,请参见控制台上传文件。
已开通智能媒体管理服务。具体操作,请参见开通产品。
已通过智能媒体管理控制台创建项目。具体操作,请参见创建项目。
说明您也可以调用API接口创建项目。具体操作,请参见CreateProject - 创建项目。
您可以调用ListProjects - 列出所有项目信息的列表接口列出指定地域下已创建的所有项目信息。
安装SDK
调用2020-09-30版本的API,需要使用2020-09-30版本的SDK。
使用智能媒体管理PHP SDK的操作步骤请参见快速入门。
安装composer
下载安装脚本
composer-setup.php
到当前目录。php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
执行安装过程。
php composer-setup.php
删除安装脚本。
php -r "unlink('composer-setup.php');"
执行
sudo mv composer.phar /usr/local/bin/composer
全局安装,完成后会有composer.phar
文件,运行php composer.phar
就可以查看composer。
安装IMM依赖
执行命令composer require alibabacloud/imm-20200930 4.4.0
。
访问凭证
您可以选择以下类型的访问凭证。
临时访问凭证:对于需要高安全性的场景,例如临时授权应用访问IMM,建议使用临时访问凭证。临时访问凭证可以限制访问的有效期,从而减少访问凭证泄露的风险。此外,临时访问凭证支持权限控制,可以有效地避免权限过大的问题。
长期访问凭证:出于安全性考虑,不建议您使用长期访问凭证,建议您使用临时访问凭证。对于需要便利性的场景,长期访问凭证可以在较长时间内免除多次刷新的麻烦。建议每三个月更换一次长期访问凭证,以提高账号的安全性。当长期访问凭证泄露或者不再使用时,应该及时删除或者禁用相关的访问凭证,以免造成安全风险。
使用临时访问凭证
当您准备临时使用PHP SDK访问IMM服务时,可以参考配置临时访问凭证。
如下以在华东1(杭州)地域中检测图片中的人脸及人脸信息为例,介绍如何使用临时访问凭证通过智能媒体管理PHP SDK调用对应API。
使用临时访问凭证获取STS TOKEN需要额外安装依赖:
composer require alibabacloud/sts-20150401 1.1.4
创建demo.php文件并写入如下内容。
<?php // This file is auto-generated, don't edit it. Thanks. namespace AlibabaCloud\SDK\Sample; use AlibabaCloud\SDK\Sts\V20150401\Sts; use AlibabaCloud\Tea\Utils\Utils; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use AlibabaCloud\SDK\Imm\V20200930\Imm; use AlibabaCloud\SDK\Imm\V20200930\Models\DetectImageFacesRequest; class Sample { /** * 使用AK&SK初始化账号Client * @return imm Client */ public static function createClient(){ // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。 $stsConfig = new Config([ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 "accessKeySecret" => getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET') ]); // Endpoint 请参考 https://api.aliyun.com/product/Sts $stsConfig->endpoint = "sts.cn-hangzhou.aliyuncs.com"; $stsClient = new Sts($stsConfig); $assumeRoleRequest = new AssumeRoleRequest([ // 要扮演的RAM角色ARN,示例值:acs:ram::123456789012****:role/adminrole "roleArn" => "acs:ram::125499367423****:role/STStokenTestRole", // 角色会话名称 "roleSessionName" => "ststest", // 凭证有效时长,单位秒 "durationSeconds" => 3600 ]); $runtime = new RuntimeOptions([]); $resp = $stsClient->assumeRoleWithOptions($assumeRoleRequest, $runtime); $config = new Config([ "accessKeyId" => $resp->body->credentials->accessKeyId, "accessKeySecret" => $resp->body->credentials->accessKeySecret, "securityToken" => $resp->body->credentials->securityToken ]); // Endpoint 请参考 https://api.aliyun.com/product/imm $config->endpoint = "imm.cn-hangzhou.aliyuncs.com"; return new Imm($config); } /** * @param string[] $args * @return void */ public static function main($args){ $client = self::createClient(); $detectImageFacesRequest = new DetectImageFacesRequest([ "projectName" => "imm-php-sdk-doc-demo", "sourceURI" => "oss://your-bucket-name/your-path/your-image.jpg" ]); $runtime = new RuntimeOptions([]); try { // 复制代码运行请自行打印 API 的返回值 print_r($client->detectImageFacesWithOptions($detectImageFacesRequest, $runtime)); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message var_dump($error->message); // 诊断地址 var_dump($error->data["Recommend"]); Utils::assertAsString($error->message); } } } $path = __DIR__ . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php'; if (file_exists($path)) { require_once $path; } Sample::main(array_slice($argv, 1));
执行
php demo.php
命令,控制台输出结果如下。AlibabaCloud\SDK\Imm\V20200930\Models\DetectImageFacesResponse Object ( [headers] => Array ( [Date] => Array ( [0] => Thu, 18 Jul 2024 03:42:20 GMT ) [Content-Type] => Array ( [0] => application/json;charset=utf-8 ) [Content-Length] => Array ( [0] => 680 ) [Connection] => Array ( [0] => keep-alive ) [Keep-Alive] => Array ( [0] => timeout=25 ) [Access-Control-Allow-Origin] => Array ( [0] => * ) [Access-Control-Expose-Headers] => Array ( [0] => * ) [x-acs-request-id] => Array ( [0] => 597781D7-B528-58BE-994A-EB45A845AB32 ) [x-acs-trace-id] => Array ( [0] => 305d56e78b78b8e26bb6f71d4c56778c ) [ETag] => Array ( [0] => 6G4qBOpKbiH3Vxe6OzA6yRw0 ) ) [statusCode] => 200 [body] => AlibabaCloud\SDK\Imm\V20200930\Models\DetectImageFacesResponseBody Object ( [faces] => Array ( [0] => AlibabaCloud\SDK\Imm\V20200930\Models\Figure Object ( [age] => 45 [ageSD] => 7 [attractive] => 0.044 [beard] => none [beardConfidence] => 0.987 [boundary] => AlibabaCloud\SDK\Imm\V20200930\Models\Boundary Object ( [height] => 381 [left] => 182 [polygon] => [top] => 175 [width] => 304 [_name:protected] => Array ( [height] => Height [left] => Left [polygon] => Polygon [top] => Top [width] => Width ) [_required:protected] => Array ( ) ) [emotion] => happiness [emotionConfidence] => 0.984 [faceQuality] => 0.942 [figureClusterConfidence] => [figureClusterId] => figure-cluster-id-unavailable [figureConfidence] => 1 [figureId] => b641a062-0f29-4bf3-b2f6-0f1972a260a3 [figureType] => face [gender] => female [genderConfidence] => 1 [glasses] => glasses [glassesConfidence] => 0.976 [hat] => none [hatConfidence] => 1 [headPose] => AlibabaCloud\SDK\Imm\V20200930\Models\HeadPose Object ( [pitch] => -16.206 [roll] => -5.124 [yaw] => 3.421 [_name:protected] => Array ( [pitch] => Pitch [roll] => Roll [yaw] => Yaw ) [_required:protected] => Array ( ) ) [mask] => none [maskConfidence] => 0.764 [mouth] => open [mouthConfidence] => 0.999 [sharpness] => 1 [_name:protected] => Array ( [age] => Age [ageSD] => AgeSD [attractive] => Attractive [beard] => Beard [beardConfidence] => BeardConfidence [boundary] => Boundary [emotion] => Emotion [emotionConfidence] => EmotionConfidence [faceQuality] => FaceQuality [figureClusterConfidence] => FigureClusterConfidence [figureClusterId] => FigureClusterId [figureConfidence] => FigureConfidence [figureId] => FigureId [figureType] => FigureType [gender] => Gender [genderConfidence] => GenderConfidence [glasses] => Glasses [glassesConfidence] => GlassesConfidence [hat] => Hat [hatConfidence] => HatConfidence [headPose] => HeadPose [mask] => Mask [maskConfidence] => MaskConfidence [mouth] => Mouth [mouthConfidence] => MouthConfidence [sharpness] => Sharpness ) [_required:protected] => Array ( ) ) ) [requestId] => 597781D7-B528-58BE-994A-EB45A845AB32 [_name:protected] => Array ( [faces] => Faces [requestId] => RequestId ) [_required:protected] => Array ( ) ) [_name:protected] => Array ( [headers] => headers [statusCode] => statusCode [body] => body ) [_required:protected] => Array ( ) )
使用长期访问凭证
当您准备临时使用PHP SDK访问IMM服务时,可以参考配置长期访问凭证。
如下以在华东1(杭州)地域中检测图片中的人脸及人脸信息为例,介绍如何使用长期访问凭证通过智能媒体管理PHP SDK调用对应API。
创建demo.php文件并写入如下内容。
<?php // This file is auto-generated, don't edit it. Thanks. namespace AlibabaCloud\SDK\Sample; use AlibabaCloud\SDK\Imm\V20200930\Imm; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use AlibabaCloud\Tea\Utils\Utils; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Imm\V20200930\Models\DetectImageFacesRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; class Sample { /** * 使用AK&SK初始化账号Client * @return Imm Client */ public static function createClient(){ // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。 $config = new Config([ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") ]); // Endpoint 请参考 https://api.aliyun.com/product/imm $config->endpoint = "imm.cn-beijing.aliyuncs.com"; return new Imm($config); } /** * @param string[] $args * @return void */ public static function main($args){ $client = self::createClient(); $detectImageFacesRequest = new DetectImageFacesRequest([ "projectName" => "imm-php-sdk-doc-demo", "sourceURI" => "oss://your-bucket-name/your-path/your-image.jpg" ]); $runtime = new RuntimeOptions([]); try { // 复制代码运行请自行打印 API 的返回值 print_r($client->detectImageFacesWithOptions($detectImageFacesRequest, $runtime)); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message var_dump($error->message); // 诊断地址 var_dump($error->data["Recommend"]); Utils::assertAsString($error->message); } } } $path = __DIR__ . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php'; if (file_exists($path)) { require_once $path; } Sample::main(array_slice($argv, 1));
执行
php demo.php
命令,控制台输出结果如下。Array ( [Faces] => Array ( [0] => Array ( [Age] => 12 [AgeSD] => 5 [Attractive] => 0.12099999934435 [Beard] => none [BeardConfidence] => 0.99800002574921 [Boundary] => Array ( [Height] => 87 [Left] => 280 [Top] => 36 [Width] => 70 ) [Emotion] => none [EmotionConfidence] => 0.99500000476837 [FaceQuality] => 0.8289999961853 [FigureClusterId] => figure-cluster-id-unavailable [FigureConfidence] => 0.9990000128746 [FigureId] => 3acab8ff-48e7-4647-ac57-b48834db7278 [FigureType] => face [Gender] => male [GenderConfidence] => 0.95099997520447 [Glasses] => none [GlassesConfidence] => 1 [Hat] => hat [HatConfidence] => 0.9879999756813 [HeadPose] => Array ( [Pitch] => -12.309000015259 [Roll] => -11.362000465393 [Yaw] => 3.4430000782013 ) [Mask] => none [MaskConfidence] => 0.7509999871254 [Mouth] => open [MouthConfidence] => 0.56699997186661 [Sharpness] => 0.8870000243187 ) ) [RequestId] => F6D3FE7E-F0D7-0A5B-AE34-7453991DF9F2 )