全部产品

PHP SDK

更新时间:2021-05-08 13:50:38

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey

  2. 安装PHP SDK核心库。使用依赖包工具安装(推荐)。执行以下命令,安装视频指纹SDK库:

  3. composer require alibabacloud/videosearch-20200225
    (最新版本1.1.2)

    详情请参考Alibaba Cloud SDK for PHP或自行添加解决方案。

完整代码示例

<?php
require __DIR__ .'/vendor/autoload.php';

useAlibabaCloud\SDK\Videosearch\V20200225\Models\AddDeletionVideoTaskRequest;
useAlibabaCloud\SDK\Videosearch\V20200225\Models\AddSearchVideoTaskRequest;
useAlibabaCloud\SDK\Videosearch\V20200225\Models\AddStorageVideoTaskRequest;
useAlibabaCloud\SDK\Videosearch\V20200225\Models\GetStorageHistoryRequest;
useAlibabaCloud\SDK\Videosearch\V20200225\Models\GetTaskStatusRequest;
useAlibabaCloud\SDK\Videosearch\V20200225\Videosearch;
useAlibabaCloud\Tea\Exception\TeaUnableRetryError;
useAlibabaCloud\Tea\Rpc\Rpc\Config;

$config =newConfig();
$config->accessKeyId ="<yourAccessKeyId>";
$config->accessKeySecret ="<yourAccessKeySecret>";
$config->regionId ="<yourRegionId>";
$config->endpoint ="multisearch.<regionId>.aliyuncs.com";

$client =newVideosearch($config);

// 创建入库任务
$request =newAddStorageVideoTaskRequest();
// 实例id
$request->instanceId ="<yourInstanceId>";
// 视频url
$request->videoUrl ="<yourVideoUrl>";
// 视频id(业务key)
$request->videoId ="<yourVideoId>";
// 视频描述
$request->description ="<yourDescription>";
// 视频标签
$request->videoTags ="<yourVideoTag>";
// 入库完成的回调
$request->callbackUrl ="<yourCallbackUrl>";

try{
    $response = $client->addStorageVideoTask($request);
    var_dump($response->toMap());
}catch(TeaUnableRetryError $e){
    var_dump($e->getLastException(), $e->getLastRequest());
}

// 创建检索任务
$request =newAddSearchVideoTaskRequest();

// 实例id
$request->instanceId ="<yourInstanceId>";
// 检索类型(1. 视频搜视频   2. 图片搜视频)
$request->searchType =1;
// 视频/图片的url地址
//如果需要使用 本地文件上传的功能,则用xxxFileObject参数并且使用 AddSearchXXXTaskAdvanceRequest对象
$request->videoUrl ="<yourVideoUrl>";
// 视频/图片的描述
$request->description ="<yourDescription>";
// 检索标签
$request->queryTags ="<yourQueryTags>";
// 返回结果数
$request->returnVideoNumber =20;
// 查询入库类型,当检索类型为1时生效(1. 直接入库  2. 去重入库  3. 不入库)
$request->storageType =3;
// 视频id,当searchType为1且storageType为1,2时生效
$request->videoId ="<yourVideoId>"
// 视频标签,当searchType为1且storageType为1,2时生效
$request->videoTags ="<yourVideoTags>";
// 去重入库阈值,searchType为1且storageType为2时生效
$request->replaceStorageThreshold =0.75;
// 检索完成的回调
$request->callbackUrl ="<yourCallbackUrl>";

try{
    $response = $client->addSearchVideoTask($request);
    var_dump($response->toMap());
}catch(TeaUnableRetryError $e){
    var_dump($e->getLastException(), $e->getLastRequest());
}

// 获取任务状态
$request =newGetTaskStatusRequest();
// 实例id
$request->instanceId ="<yourInstanceId>";
// 任务id
$request->taskId ="<yourTaskId>";

try{
    $response = $client->getTaskStatus($request);
    var_dump($response->toMap());
}catch(TeaUnableRetryError $e){
    var_dump($e->getLastException(), $e->getLastRequest());
}

// 获取入库历史
$request =newGetStorageHistoryRequest();
// 实例id
$request->instanceId ="<yourInstanceId>";
// 视频id
$request->videoId ="<yourVideoId>";
// 当前页数
$request->pageNumber =1;
// 分页大小
$request->pageSize =10;

try{
    $response = $client->getStorageHistory($request);
    var_dump($response->toMap());
}catch(TeaUnableRetryError $e){
    var_dump($e->getLastException(), $e->getLastRequest());
}

// 删除任务
$request =newAddDeletionVideoTaskRequest();
// 实例id
$request->instanceId ="<yourInstanceId>";
// 视频id
$request->videoId ="<yourVideoId>";

try{
    $response = $client->addDeletionVideoTask($request);
    var_dump($response->toMap());
}catch(TeaUnableRetryError $e){
    var_dump($e->getLastException(), $e->getLastRequest());
}

//查询批量任务
$request = new \AlibabaCloud\SDK\Videosearch\V20200225\Models\ListBatchTaskRequest();
$request->instanceId = "<yourInstanceId>";

try {

    $response = $client->listBatchTask($request,new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions());
    var_dump($response->toMap());
    exit;
} catch (TeaUnableRetryError $e) {
    var_dump($e->getLastException(), $e->getLastRequest());
}

//创建批量任务

$request = new \AlibabaCloud\SDK\Videosearch\V20200225\Models\CreateBatchTaskRequest();
$request->batchTaskType = "<yourBatchTaskType>";
$request->instanceId = "<yourInstanceId>";
$request->roleArn = "<yourRoleArn>";
$request->ossBucketName = "<yourBucketName>";
$request->ossDataPath = "<yourDataPath>";
$request->ossMetaFile = "<yourOssMetaFile>";

try {

    $response = $client->createBatchTask($request,new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions());
    var_dump($response->toMap());
    exit;
} catch (TeaUnableRetryError $e) {
    var_dump($e->getLastException(), $e->getLastRequest());
}

//查询音频实例检索任务列表
$request = new \AlibabaCloud\SDK\Videosearch\V20200225\Models\ListSearchAudioTasksRequest();
$request->instanceId = "<yourInstanceId>";

try {
  
    $response = $client->listSearchAudioTasks($request,new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions());
    var_dump($response->toMap());
    exit;
} catch (TeaUnableRetryError $e) {
    var_dump($e->getLastException(), $e->getLastRequest());
}

//查询音频实例检索任务列表
$request = new \AlibabaCloud\SDK\Videosearch\V20200225\Models\ListStorageAudioTasksRequest();
$request->instanceId = "<yourInstanceId>";

try {
   
    $response = $client->listStorageAudioTasks($request,new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions());
    var_dump($response->toMap());
    exit;
} catch (TeaUnableRetryError $e) {
    var_dump($e->getLastException(), $e->getLastRequest());
}