edit-icon download-icon

Basic video attributes

Last Updated: Feb 11, 2018

Back to Overview

Media libraries - Media set basic information - Sample code - PHP

  • Overview

    The following example describes how to query and update the basic information of a media set.

    For details about how to install and use the SDK, see Media library SDK-PHP.

  • Query the basic information of a media set

    You can use the media ID or OSS file URL to query a media set.

    • Query a media set by media ID

      For details about the parameters, see API reference > Media APIs > Query media sets by media IDs.

      1. include_once 'aliyun-php-sdk-core/Config.php';
      2. use Mts\Request\V20140618 as Mts;
      3. $accessKeyID = 'test'; // Replace the value with your AccessKeyID
      4. $accessKeySecret = 'test'; // Replace the value with your AccessKeySecret
      5. $profile = DefaultProfile::getProfile('cn-hangzhou',
      6. $accessKeyID,
      7. $accessKeySecret);
      8. $client = new DefaultAcsClient($profile);
      1. function queryMediaById($client, $mediaID)
      2. {
      3. $request = new Mts\QueryMediaListRequest();
      4. $request->setAcceptFormat('JSON');
      5. $request->setMediaIds($mediaID);
      6. $response = $client->getAcsResponse($request);
      7. return $response;
      8. }
      9. function printMedia($media)
      10. {
      11. if (array_key_exists('Title', $media)) {
      12. print_r('Title: '.$media->{'Title'}."\n");
      13. }
      14. if (array_key_exists('Description', $media)) {
      15. print_r('Description: '.$media->{'Description'}."\n");
      16. }
      17. if (array_key_exists('Tags', $media)) {
      18. print_r('Tags: '.$media->{'Tags'}->{'Tag'}[0]."\n");
      19. }
      20. if (array_key_exists('CoverURL', $media)) {
      21. print_r('CoverURL: '.$media->{'CoverURL'}."\n");
      22. }
      23. print_r('Format: '.$media->{'Format'}."\n");
      24. print_r('Resolution: '.$media->{'Width'}.'x'.$media->{'Height'}."\n");
      25. print_r('FileSize: '.$media->{'Size'}."\n");
      26. print_r('Bitrate: '.$media->{'Bitrate'}."\n");
      27. print_r('FPS: '.$media->{'Fps'}."\n");
      28. }
      29. $mediaID = 'test'; // Replace the value with your desired media ID
      30. $medias = queryMediaById($client, $mediaID)->{'MediaList'}->{'Media'};
      31. for ($i=0; $i < count($medias); $i++) {
      32. printMedia($medias[$i]);
      33. }
    • Query a media set by an OSS file URL

      For details about the parameters, see API reference > Media APIs > Query media sets by URLs.

      1. function queryMediaByURL($client, $mediaURL)
      2. {
      3. $request = new Mts\QueryMediaListByURLRequest();
      4. $request->setAcceptFormat('JSON');
      5. $request->setFileURLs($mediaURL);
      6. $response = $client->getAcsResponse($request);
      7. return $response;
      8. }
      9. $ossEndpoint = 'http://test.oss-cn-hangzhou.aliyuncs.com/';
      10. // An OSS object does not have to start with "/". Replace the value with your OSS object
      11. $ossObject = 'test/test.mp4';
      12. $medias = queryMediaByURL($client,$ossEndpoint.urlencode($ossObject))->{'MediaList'}->{'Media'};
      13. for ($i=0; $i < count($medias); $i++) {
      14. printMedia($medias[$i]);
      15. }
  • Update attributes

    You can update full attributes or a single attribute.

    • Full attribute update

      For details about the parameters, see API reference > Media APIs > Update media set basic information.

      Specify all fields when updating attributes. Fields not set are cleared.

      1. function updateMediaAllField($client, $mediaID, $title, $description, $tags, $coverURL)
      2. {
      3. $request = new Mts\UpdateMediaRequest();
      4. $request->setAcceptFormat('JSON');
      5. $request->setMediaId($mediaID);
      6. $request->setTitle($title);
      7. $request->setCateId(2663987);
      8. $request->setDescription($description);
      9. $request->setTags($tags);
      10. $request->setCoverURL($coverURL);
      11. $response = $client->getAcsResponse($request);
      12. return $response;
      13. }
      14. $mediaID = 'test'; // Replace the value with your desired media ID
      15. $media = updateMediaAllField($client, $mediaID,
      16. 'title', 'description', 'tags', 'coverURL')->{'Media'};
    • Single attribute update

      You can use different APIs to conveniently update single fields without modifying other fields.

      The following section uses the “publishing state” as an example. For details about the parameters, see API reference > Media APIs > Update media publishing state.

      1. function updateMediaPublishState($client, $mediaID, $state)
      2. {
      3. $request = new Mts\UpdateMediaPublishStateRequest();
      4. $request->setAcceptFormat('JSON');
      5. $request->setMediaId($mediaID);
      6. $request->setPublish($state);
      7. $response = $client->getAcsResponse($request);
      8. return $response;
      9. }
      10. $mediaID = 'test'; // Replace the value with your desired media ID
      11. // No result is returned from the API that updates the publishing state. Capture exceptions to check whether execution succeeds
      12. try {
      13. updateMediaPublishState($client, $mediaID, "true");
      14. } catch (ClientException $e) {
      15. print_r('ClientException:'."\n");
      16. print_r($e);
      17. } catch (ServerException $e) {
      18. print_r('ServerException:'."\n");
      19. print_r($e);
      20. }
Thank you! We've received your feedback.