全部产品
云市场
云游戏

PHP SDK

更新时间:2020-04-24 19:21:49

准备工作

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

  2. 安装PHP SDK核心库。 使用依赖包工具安装(推荐)。

    执行以下命令,安装阿里云SDK核心库:

    1. composer require alibabacloud/sdk

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

  3. 安装多媒体AI搜索PHP SDK。

    使用依赖包工具安装(推荐)。

    执行以下命令,安装多媒体AI PHP SDK:

    1. composer require alibabacloud/multimediaai

    自行下载安装。

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

注意:模板中yourContent 参考 模板附录

完整代码示例

  1. <?php
  2. //用户根据自己下载包的情况进行引用
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Multimediaai\Multimediaai;
  7. AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
  8. ->regionId('<region>')
  9. ->asDefaultClient();
  10. // 创建结构化任务
  11. $request = Multimediaai::v20190810() -> createLabelTask();
  12. try {
  13. $result = $request->withVideoUrl("<yourVideoUrl>") // 需要分析视频url
  14. ->withVideoName("<yourVideoName>") // 视频名称,推荐传入视频标题,可提升视频分类准确度
  15. ->withApplicationId("<yourApplicationId>") // 通过控制台获取应用Id
  16. // ->withCallbackUrl("<yourCallbackUrl>") // 回调通知Url,此参数为可选
  17. // ->withTemplateId("<yourTemplateId>") // 自定义模板id,此参数为可选,不传会使用默认模板
  18. ->request();
  19. print_r($result);
  20. } catch(ServerException $e) {
  21. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  22. } catch(ClientException $e) {
  23. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  24. }
  25. // 创建视频首图任务
  26. $request = Multimediaai::v20190810() -> createCoverTask();
  27. try {
  28. $result = $request->withVideoUrl("<yourVideoUrl>") // 需要分析视频url
  29. ->withVideoName("<yourVideoName>") // 视频名称
  30. ->withApplicationId("<yourApplicationId>") // 通过控制台获取应用Id
  31. // ->withCallbackUrl("<yourCallbackUrl>") // 回调通知Url,此参数为可选
  32. // ->withTemplateId("<yourTemplateId>") // 自定义模板id,此参数为可选,不传会使用默认模板
  33. ->request();
  34. print_r($result);
  35. } catch(ServerException $e) {
  36. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  37. } catch(ClientException $e) {
  38. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  39. }
  40. // 创建视频GIF任务
  41. $request = Multimediaai::v20190810() -> createGifTask();
  42. try {
  43. $result = $request->withVideoUrl("<yourVideoUrl>") // 需要分析视频url
  44. ->withVideoName("<yourVideoName>") // 视频名称
  45. ->withApplicationId("<yourApplicationId>") // 通过控制台获取应用Id
  46. // ->withCallbackUrl("<yourCallbackUrl>") // 回调通知Url,此参数为可选
  47. // ->withTemplateId("<yourTemplateId>") // 自定义模板id,此参数为可选,不传会使用默认模板
  48. ->request();
  49. print_r($result);
  50. } catch(ServerException $e) {
  51. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  52. } catch(ClientException $e) {
  53. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  54. }
  55. // 获取任务处理状态
  56. $request = Multimediaai::v20190810() -> getTaskStatus();
  57. try {
  58. $result = $request->withTaskId(<yourTaskId>) // 任务id
  59. ->request();
  60. print_r($result);
  61. } catch(ServerException $e) {
  62. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  63. } catch(ClientException $e) {
  64. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  65. }
  66. // 获取任务处理结果
  67. $request = Multimediaai::v20190810() -> getTaskResult();
  68. try {
  69. $result = $request->withTaskId(<yourTaskId>) // 任务id
  70. ->request();
  71. print_r($result);
  72. } catch(ServerException $e) {
  73. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  74. } catch(ClientException $e) {
  75. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  76. }
  77. // 创建人脸库
  78. $request = Multimediaai::v20190810() -> createFaceGroup();
  79. try {
  80. $result = $request->withFaceGroupName(<yourFaceGroupName>) // 人脸库名称
  81. ->withDescription(<yourDescription>) // 描述
  82. ->request();
  83. print_r($result);
  84. } catch(ServerException $e) {
  85. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  86. } catch(ClientException $e) {
  87. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  88. }
  89. // 创建人脸库人物
  90. $request = Multimediaai::v20190810() -> createFacePerson();
  91. try {
  92. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  93. ->withFacePersonName(<yourFacePersonName>) // 人脸库人物名称
  94. ->request();
  95. print_r($result);
  96. } catch(ServerException $e) {
  97. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  98. } catch(ClientException $e) {
  99. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  100. }
  101. // 注册人脸
  102. $request = Multimediaai::v20190810() -> registerFaceImage();
  103. try {
  104. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  105. ->withFacePersonId(<yourFacePersonId>) // 人脸库人物id
  106. ->withImageUrl(<yourTaskId>) // 人脸图片Url
  107. ->request();
  108. print_r($result);
  109. } catch(ServerException $e) {
  110. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  111. } catch(ClientException $e) {
  112. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  113. }
  114. // 删除人脸库
  115. $request = Multimediaai::v20190810() -> deleteFaceGroup();
  116. try {
  117. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  118. ->request();
  119. print_r($result);
  120. } catch(ServerException $e) {
  121. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  122. } catch(ClientException $e) {
  123. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  124. }
  125. // 删除人脸库人物
  126. $request = Multimediaai::v20190810() -> deleteFacePerson();
  127. try {
  128. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  129. ->withFacePersonId(<yourFacePersonId>) // 人脸库图片id
  130. ->request();
  131. print_r($result);
  132. } catch(ServerException $e) {
  133. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  134. } catch(ClientException $e) {
  135. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  136. }
  137. // 删除人脸库人物图片
  138. $request = Multimediaai::v20190810() -> deleteFaceImage();
  139. try {
  140. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  141. ->withFacePersonId(<yourFacePersonId>) // 人脸库图片id
  142. ->withImageUrl(<yourImageUrl>) // 人脸库图片id
  143. ->request();
  144. print_r($result);
  145. } catch(ServerException $e) {
  146. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  147. } catch(ClientException $e) {
  148. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  149. }
  150. // 获取人脸库列表
  151. $request = Multimediaai::v20190810() -> listFaceGroups();
  152. try {
  153. $result = $request->withPageNumber(<yourPageNumber>) // 页码
  154. ->withPageSize(<yourPageSize>) // 每页数量
  155. ->request();
  156. print_r($result);
  157. } catch(ServerException $e) {
  158. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  159. } catch(ClientException $e) {
  160. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  161. }
  162. // 获取人脸库人物列表
  163. $request = Multimediaai::v20190810() -> listFacePersons();
  164. try {
  165. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  166. ->withPageNumber(<yourPageNumber>) // 页码
  167. ->withPageSize(<yourPageSize>) // 每页数量
  168. ->request();
  169. print_r($result);
  170. } catch(ServerException $e) {
  171. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  172. } catch(ClientException $e) {
  173. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  174. }
  175. // 获取人脸库人物图片列表
  176. $request = Multimediaai::v20190810() -> listFaceImages();
  177. try {
  178. $result = $request->withFaceGroupId(<yourFaceGroupId>) // 人脸库id
  179. ->withFaceGroupId(<yourFaceGroupId>) // 人脸库人物id
  180. ->withPageNumber(<yourPageNumber>) // 页码
  181. ->withPageSize(<yourPageSize>) // 每页数量
  182. ->request();
  183. print_r($result);
  184. } catch(ServerException $e) {
  185. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  186. } catch(ClientException $e) {
  187. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  188. }
  189. // 创建视频内容分析模板
  190. $request = Multimediaai::v20190810() -> CreateTemplate();
  191. try {
  192. $result = $request->withTemplateName(<yourTemplateName>) // 模板名称
  193. ->withTemplateContent(<yourContent>) // 模板内容
  194. ->withIsDefault(false) //是否默认模板(true 是 false 否)
  195. ->withType(1) //1.LABEL (默认填1)
  196. ->request();
  197. print_r($result);
  198. } catch(ServerException $e) {
  199. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  200. } catch(ClientException $e) {
  201. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  202. }
  203. // 修改视频内容分析模板
  204. $request = Multimediaai::v20190810() -> UpdateTemplate();
  205. try {
  206. $result = $request->withTemplateName(<yourTemplateName>) // 模板名称
  207. ->withTemplateId(<yourTemplateName>) // 模板id
  208. ->withTemplateContent(<yourContent>) // 模板内容
  209. ->withIsDefault(false) //是否默认模板(true 是 false 否)
  210. ->withType(1) //1.LABEL (默认填1)
  211. ->request();
  212. print_r($result);
  213. } catch(ServerException $e) {
  214. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  215. } catch(ClientException $e) {
  216. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  217. }
  218. // 查询视频内容分析模板列表
  219. $request = Multimediaai::v20190810() -> ListTemplates();
  220. try {
  221. $result = $request->withTemplateName(<yourTemplateName>) // 模板名称
  222. ->withTemplateId(<yourTemplateName>) // 模板id
  223. ->withType(1) //1.LABEL (默认填1)
  224. ->withPageNumber(<yourPageNumber>) // 页码
  225. ->withPageSize(<yourPageSize>) // 每页数量
  226. ->request();
  227. print_r($result);
  228. } catch(ServerException $e) {
  229. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  230. } catch(ClientException $e) {
  231. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  232. }
  233. // 查询视频内容分析模板详情
  234. $request = Multimediaai::v20190810() -> GetTemplate();
  235. try {
  236. $result = $request->withTemplateId(<yourTemplateName>) // 模板id
  237. ->request();
  238. print_r($result);
  239. } catch(ServerException $e) {
  240. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  241. } catch(ClientException $e) {
  242. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  243. }