简介

SDK的安装和使用,参见 媒体库SDK-PHP

类目支持增删改查。您需要关注以下内容:

  • 删除一个类目并不会自动清除关联媒体的类目ID属性。
  • 查询类目返回接口有两种形式:树结构、列表结构。树结构返回的是一个嵌套结构的JSON对象,列表结构返回的是一个平面的数组结构,可以根据场景选择使用。

新增类目

参见 API使用手册 > 媒体类目接口 > 新增类目
include_once 'aliyun-php-sdk-core/Config.php';
  use Mts\Request\V20140618 as Mts;
  $accessKeyID = 'test'; // 替换成真实的id
  $accessKeySecret = 'test'; // 替换成真实的secret
  $profile = DefaultProfile::getProfile('cn-hangzhou',
                                        $accessKeyID,
                                        $accessKeySecret);
  $client = new DefaultAcsClient($profile);
function addCategory($client, $parentId, $categoryName)
  {
      $request = new Mts\AddCategoryRequest();
      $request->setAcceptFormat('JSON');
      $request->setParentId($parentId);
      $request->setCateName($categoryName);
      $response = $client->getAcsResponse($request);
      return $response;
  }
  $category = addCategory($client, null, 'testroot')->{'Category'};
  print_r('Level: '.$category->{'Level'}.
          "\tParentId: ".$category->{'ParentId'}.
          "\tCateId: ".$category->{'CateId'}.
          "\tCateName: ".$category->{'CateName'}."\n");

更新类目

参见 API使用手册 > 媒体类目接口 > 更新类目
function updateCategory($client, $categoryId, $categoryName)
  {
      $request = new Mts\UpdateCategoryNameRequest();
      $request->setAcceptFormat('JSON');
      $request->setCateId($categoryId);
      $request->setCateName($categoryName);
      $response = $client->getAcsResponse($request);
      return $response;
  }
  try {
      updateCategory($client, 12345678, 'updatetestroot'); // 替换成真实的CateID
  } catch (ClientException $e) {
      print_r('ClientException:'."\n");
      print_r($e);
  } catch (ServerException $e) {
      print_r('ServerException:'."\n");
      print_r($e);
  }

删除类目

参见 API使用手册 > 媒体类目接口 > 删除类目
function deleteCategory($client, $categoryId)
  {
      $request = new Mts\DeleteCategoryRequest();
      $request->setAcceptFormat('JSON');
      $request->setCateId($categoryId);
      $response = $client->getAcsResponse($request);
      return $response;
  }
  try {
      deleteCategory($client, 12345678); // 替换成真实的CateID
  } catch (ClientException $e) {
      print_r('ClientException:'."\n");
      print_r($e);
  } catch (ServerException $e) {
      print_r('ServerException:'."\n");
      print_r($e);
  }

查询类目

  • 树结构
    参见 API使用手册 > 媒体类目接口 > 查询类目 > 树
    function queryCategoryTree($client)
        {
            $request = new Mts\CategoryTreeRequest();
            $request->setAcceptFormat('JSON');
            $response = $client->getAcsResponse($request);
            return $response;
        }
        function printCategoryTree($categoryTree)
        {
            foreach($categoryTree as $category) {
                for ($i = 0; $i < $category->{'Level'}; $i++) {
                    print_r("--");
                }
                print_r('Level: '.$category->{'Level'}.
                        "\tParentId: ".$category->{'ParentId'}.
                        "\tCateId: ".$category->{'CateId'}.
                        "\tCateName: ".$category->{'CateName'}."\n");
                if (array_key_exists('SubcateList', $category)) {
                    printCategoryTree($category->{'SubcateList'});
                }
            }
        }
        $categoryTree = queryCategoryTree($client)->{'CategoryTree'};
        printCategoryTree(json_decode($categoryTree));
  • 列表结构参见 API使用手册 > 媒体类目接口 > 查询类目 > 列表
     function queryCategoryList($client)
         {
            $request = new Mts\ListAllCategoryRequest();
            $request->setAcceptFormat('JSON');
            $response = $client->getAcsResponse($request);
            return $response ;
        }
        $categoryList = queryCategoryList($client)->{'CategoryList'}->{'Category'};
        for ($i = 0; $i < count($categoryList); $i++) {
            print_r('Level: '.$categoryList[$i]->{'Level'}.
                    "\tParentId: ".$categoryList[$i]->{'ParentId'}.
                    "\tCateId: ".$categoryList[$i]->{'CateId'}.
                    "\tCateName: ".$categoryList[$i]->{'CateName '}."\n");
        }