edit-icon download-icon

Category management

Last Updated: Feb 14, 2018

Back to Overview

  • Overview

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

    You can add, delete, modify, and query a category. In addition, pay attention to the following logic:

    • Deleting a category does not automatically clear the category ID of an associated media set.

    • The result returned from the category query API can be displayed in the tree structure or list structure. A nested JSON object is returned in the tree structure, while a plane array is returned in the list structure. You can select a structure based on the actual scenario.

  • Add a category

    For details about the parameters, see API reference > Media category APIs > Add a category.

    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 addCategory($client, $parentId, $categoryName)
    2. {
    3. $request = new Mts\AddCategoryRequest();
    4. $request->setAcceptFormat('JSON');
    5. $request->setParentId($parentId);
    6. $request->setCateName($categoryName);
    7. $response = $client->getAcsResponse($request);
    8. return $response;
    9. }
    10. $category = addCategory($client, null, 'testroot')->{'Category'};
    11. print_r('Level: '.$category->{'Level'}.
    12. "\tParentId: ".$category->{'ParentId'}.
    13. "\tCateId: ".$category->{'CateId'}.
    14. "\tCateName: ".$category->{'CateName'}."\n");
  • Update a category

    For details about the parameters, see API reference > Media category APIs > Update a category name.

    1. function updateCategory($client, $categoryId, $categoryName)
    2. {
    3. $request = new Mts\UpdateCategoryNameRequest();
    4. $request->setAcceptFormat('JSON');
    5. $request->setCateId($categoryId);
    6. $request->setCateName($categoryName);
    7. $response = $client->getAcsResponse($request);
    8. return $response;
    9. }
    10. try {
    11. updateCategory($client, 12345678, 'updatetestroot'); // Replace the value with your category ID
    12. } catch (ClientException $e) {
    13. print_r('ClientException:'."\n");
    14. print_r($e);
    15. } catch (ServerException $e) {
    16. print_r('ServerException:'."\n");
    17. print_r($e);
    18. }
  • Delete a category

    For details about the parameters, see API reference > Media category APIs > Delete a category.

    1. function deleteCategory($client, $categoryId)
    2. {
    3. $request = new Mts\DeleteCategoryRequest();
    4. $request->setAcceptFormat('JSON');
    5. $request->setCateId($categoryId);
    6. $response = $client->getAcsResponse($request);
    7. return $response;
    8. }
    9. try {
    10. deleteCategory($client, 12345678); // Replace the value with your category ID
    11. } catch (ClientException $e) {
    12. print_r('ClientException:'."\n");
    13. print_r($e);
    14. } catch (ServerException $e) {
    15. print_r('ServerException:'."\n");
    16. print_r($e);
    17. }
  • Query a category

    • Tree structure

      For details about the parameters, see API reference > Media category APIs > Retrieve a category tree .

      1. function queryCategoryTree($client)
      2. {
      3. $request = new Mts\CategoryTreeRequest();
      4. $request->setAcceptFormat('JSON');
      5. $response = $client->getAcsResponse($request);
      6. return $response;
      7. }
      8. function printCategoryTree($categoryTree)
      9. {
      10. foreach($categoryTree as $category) {
      11. for ($i = 0; $i < $category->{'Level'}; $i++) {
      12. print_r("--");
      13. }
      14. print_r('Level: '.$category->{'Level'}.
      15. "\tParentId: ".$category->{'ParentId'}.
      16. "\tCateId: ".$category->{'CateId'}.
      17. "\tCateName: ".$category->{'CateName'}."\n");
      18. if (array_key_exists('SubcateList', $category)) {
      19. printCategoryTree($category->{'SubcateList'});
      20. }
      21. }
      22. }
      23. $categoryTree = queryCategoryTree($client)->{'CategoryTree'};
      24. printCategoryTree(json_decode($categoryTree));
    • List structure

      For details about the parameters, see API reference > Media category APIs > Retrieve a category list.

      1. function queryCategoryList($client)
      2. {
      3. $request = new Mts\ListAllCategoryRequest();
      4. $request->setAcceptFormat('JSON');
      5. $response = $client->getAcsResponse($request);
      6. return $response;
      7. }
      8. $categoryList = queryCategoryList($client)->{'CategoryList'}->{'Category'};
      9. for ($i = 0; $i < count($categoryList); $i++) {
      10. print_r('Level: '.$categoryList[$i]->{'Level'}.
      11. "\tParentId: ".$categoryList[$i]->{'ParentId'}.
      12. "\tCateId: ".$categoryList[$i]->{'CateId'}.
      13. "\tCateName: ".$categoryList[$i]->{'CateName'}."\n");
      14. }
Thank you! We've received your feedback.