全部产品

PHP SDK

更新时间:2020-10-15 10:41:58

准备工作

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

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

    1. composer require alibabacloud/videosearch-20200225

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

完整代码示例

  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use AlibabaCloud\SDK\Videosearch\V20200225\Models\AddDeletionVideoTaskRequest;
  4. use AlibabaCloud\SDK\Videosearch\V20200225\Models\AddSearchVideoTaskRequest;
  5. use AlibabaCloud\SDK\Videosearch\V20200225\Models\AddStorageVideoTaskRequest;
  6. use AlibabaCloud\SDK\Videosearch\V20200225\Models\GetStorageHistoryRequest;
  7. use AlibabaCloud\SDK\Videosearch\V20200225\Models\GetTaskStatusRequest;
  8. use AlibabaCloud\SDK\Videosearch\V20200225\Videosearch;
  9. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  10. use AlibabaCloud\Tea\Rpc\Rpc\Config;
  11. $config = new Config();
  12. $config->accessKeyId = "<yourAccessKeyId>";
  13. $config->accessKeySecret = "<yourAccessKeySecret>";
  14. $config->regionId = "<yourRegionId>";
  15. $config->endpoint = "multisearch.<regionId>.aliyuncs.com";
  16. $client = new Videosearch($config);
  17. // 创建入库任务
  18. $request = new AddStorageVideoTaskRequest();
  19. // 实例id
  20. $request->instanceId = "<yourInstanceId>";
  21. // 视频url
  22. $request->videoUrl = "<yourVideoUrl>";
  23. // 视频id(业务key)
  24. $request->videoId = "<yourVideoId>";
  25. // 视频描述
  26. $request->description = "<yourDescription>";
  27. // 视频标签
  28. $request->videoTags = "<yourVideoTag>";
  29. // 入库完成的回调
  30. $request->callbackUrl = "<yourCallbackUrl>";
  31. try {
  32. $response = $client->addStorageVideoTask($request);
  33. var_dump($response->toMap());
  34. } catch (TeaUnableRetryError $e) {
  35. var_dump($e->getLastException(), $e->getLastRequest());
  36. }
  37. // 创建检索任务
  38. $request = new AddSearchVideoTaskRequest();
  39. // 实例id
  40. $request->instanceId = "<yourInstanceId>";
  41. // 检索类型(1. 视频搜视频 2. 图片搜视频)
  42. $request->searchType = 1;
  43. // 视频/图片的url地址
  44. $request->videoUrl = "<yourVideoUrl>";
  45. // 视频/图片的描述
  46. $request->description = "<yourDescription>";
  47. // 检索标签
  48. $request->queryTags = "<yourQueryTags>";
  49. // 返回结果数
  50. $request->returnVideoNumber = 20;
  51. // 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  52. $request->storageType = 3;
  53. // 视频id,当searchType为1且storageType为1,2时生效
  54. $request->videoId = "<yourVideoId>"
  55. // 视频标签,当searchType为1且storageType为1,2时生效
  56. $request->videoTags = "<yourVideoTags>";
  57. // 去重入库阈值,searchType为1且storageType为2时生效
  58. $request->replaceStorageThreshold = 0.75;
  59. // 检索完成的回调
  60. $request->callbackUrl = "<yourCallbackUrl>";
  61. try {
  62. $response = $client->addSearchVideoTask($request);
  63. var_dump($response->toMap());
  64. } catch (TeaUnableRetryError $e) {
  65. var_dump($e->getLastException(), $e->getLastRequest());
  66. }
  67. // 获取任务状态
  68. $request = new GetTaskStatusRequest();
  69. // 实例id
  70. $request->instanceId = "<yourInstanceId>";
  71. // 任务id
  72. $request->taskId = "<yourTaskId>";
  73. try {
  74. $response = $client->getTaskStatus($request);
  75. var_dump($response->toMap());
  76. } catch (TeaUnableRetryError $e) {
  77. var_dump($e->getLastException(), $e->getLastRequest());
  78. }
  79. // 获取入库历史
  80. $request = new GetStorageHistoryRequest();
  81. // 实例id
  82. $request->instanceId = "<yourInstanceId>";
  83. // 视频id
  84. $request->videoId = "<yourVideoId>";
  85. // 当前页数
  86. $request->pageNumber = 1;
  87. // 分页大小
  88. $request->pageSize = 10;
  89. try {
  90. $response = $client->getStorageHistory($request);
  91. var_dump($response->toMap());
  92. } catch (TeaUnableRetryError $e) {
  93. var_dump($e->getLastException(), $e->getLastRequest());
  94. }
  95. // 删除任务
  96. $request = new AddDeletionVideoTaskRequest();
  97. // 实例id
  98. $request->instanceId = "<yourInstanceId>";
  99. // 视频id
  100. $request->videoId = "<yourVideoId>";
  101. try {
  102. $response = $client->addDeletionVideoTask($request);
  103. var_dump($response->toMap());
  104. } catch (TeaUnableRetryError $e) {
  105. var_dump($e->getLastException(), $e->getLastRequest());
  106. }