SDK Calling Example

There are two service authentication modes: This section uses Image Moderation as an example to describe how to use AK/SK-based and token-based authentication. The corresponding URI is POST /v1.0/moderation/image.

Using SDK in AK/SK-based Authentication Mode

  1. Set app_key and app_secret in the image_moderation_aksk_demo.php file. The sample code is as follows:
    1
    2
    3
    4
    5
    //CN North-Beijing1 (cn-north-4) is supported.
    init_region($region = 'cn-north-4');
     
    $app_key = "*************";
    $app_secret = "*************";
    
  2. Modify data_url or filepath in the image_moderation_aksk_demo.php file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace ./data/moderation-terrorism.jpg in filepath with the path of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      $filepath = "./data/moderation-terrorism.jpg";
      $data = file_to_base64($filepath);
      
      $result = image_content_aksk($app_key, $app_secret, $data, "", array("politics"), 0);
      echo $result;
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      $data_url = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg";
      
      $result = image_content_aksk($app_key, $app_secret, "", $data_url, array("politics"), 0);
      echo $result;
      
  3. Execute the image_moderation_aksk_demo.php file. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    Process finished with exit code 0
    

Using SDK in Token-based Authentication Mode

  1. In the image_moderation_token_demo.php file, set the registered username and password. The username of a non-IAM login user is the same as the domain name of an IAM login user. The sample code is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    //CN North-Beijing1 (cn-north-4) is supported.
    init_region($region = 'cn-north-4');
     
    $username = "********"; // Configure the username.
    $password = "********";      // Configure the password.
    $domainName = "*********"; // Configure the domain name.
    
    $token = get_token($username, $password, $domainName);
    
  2. Modify data_url or filepath of the image in the image_moderation_token_demo.php file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace ./data/moderation-terrorism.jpg in filepath with the path of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      6
      $filepath = "./data/moderation-terrorism.jpg";
      $data = file_to_base64($filepath);
      
      $result = image_content($token, $data, "", array("politics"), 0);
      echo $result;
      echo "\n";
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      $data_url = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg";
      
      $result = image_content($token, "", $data_url, array("politics"), 0);
      echo $result;
      
  3. Execute the image_moderation_token_demo.php file. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    Process finished with exit code 0