このトピックでは、Alibaba Cloud Image Search SDK for PHP の使用方法について説明します。

API 操作

操作 説明 
Add 画像を追加します。
SearchImageByPic 新しい画像に基づいて類似画像を検索します。
SearchImageByName 画像ギャラリー内の既存の画像の名前に基づいて、類似した画像を検索します。
Delete 画像を削除します。

準備

以下の Web サイトから、Alibaba Cloud Composer のイメージを取得します。
https://developer.aliyun.com/composer
依存関係をインストールします。
  • Alibaba Cloud Composer のイメージからAlibaba Cloud Image Search SDK for PHP を取得します。
    composer require alibabacloud/imagesearch-20200212
  • composer.json ファイルを使用します。
    {
       "require": {
           "alibabacloud/imagesearch-20200212": "*"    
        }
    }
依存関係に関連する以下のサンプルコードが用意されています。
declare (strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20200212\ImageSearch;
use AlibabaCloud\SDK\OpenPlatform\V20191219\OpenPlatform;
use AlibabaCloud\SDK\OpenPlatform\V20191219\OpenPlatform\AuthorizeFileUploadRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use AlibabaCloud\Tea\FileForm\FileForm\FileField;
use AlibabaCloud\Tea\Rpc\Rpc\Config;
use AlibabaCloud\Tea\RpcUtils\RpcUtils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

Add

  • サンプルコード
    function _addImage()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new ImageSearch\AddImageAdvanceRequest();
        // Required. The name of the Image Search instance.
        $request->instanceName = "xxxxxx";
        // Required. The image name, which can be up to 512 characters in length.
        // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
        // 2. If you add images that have the same ProductId and PicName for multiple times, the image that is added in the last operation overwrites the images that are added in previous operations.
        $request->productId = "test";
        $request->picName = "test";
        $request->picContentObject = new Stream(fopen("<filePath>", 'r+'));
          
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->addImageAdvance($request, $runtime);
            var_dump($response->toMap());
          } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } 
    }
  • レスポンスの例
    {
      ["requestId"]=>
      string(36) "242CB8B7-DD36-42ED-9A76-37915DC7ED81"
      ["success"]=>
      bool(true)
      ["message"]=>
      string(7) "success"
      ["code"]=>
      int(0)
      ["picInfo"]=>
      array(2) {
        ["CategoryId"]=>
        int(0)
        ["Region"]=>
        string(15) "167,477,221,407"
      }
      ["_name":protected]=>
      array(5) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["message"]=>
        string(7) "Message"
        ["code"]=>
        string(4) "Code"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

SearchImageByPic

  • サンプルコード
    function _getImageByPic()
    {
        $ config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new ImageSearch\SearchImageByPicAdvanceRequest();
        // Required. The name of the Image Search instance.
        $request->instanceName = "xxxxx";
        $imageStream = new Stream(fopen("<filePath>", 'r+'));
        $request->picContentObject = $imageStream;
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->searchImageByPicAdvance($request, $runtime);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } catch (Exception $e) {
        }
    }
  • レスポンスの例
    {
      ["requestId"]=>
      string(36) "453517A3-7D00-476E-A946-8CC3B9DC6FA1"
      ["success"]=>
      bool(true)
      ["code"]=>
      int(0)
      ["msg"]=>
      string(7) "success"
      ["auctions"]=>
      array(5) {
        [0]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(7) "vae.jpg"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
        }
        [1]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "test"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5 )
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(171)
      }
      ["picInfo"]=>
      array(3) {
        ["CategoryId"]=>
        int(0)
        ["AllCategories"]=>
        array(14) {
          [0]=>
          array(2) {
            ["Id"]=>
            int(0)
            ["Name"]=>
            string(4) "Tops"
          }
      ........
          [13]=>
          array(2) {
            ["Id"]=>
            int(88888888)
            ["Name"]=>
            string(5) "Other"
          }
        }
        ["Region"]=>
        string(15) "167,477,221,407"
      }
      ["_name":protected]=>
      array(7) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["code"]=>
        string(4) "Code"
        ["msg"]=>
        string(3) "Msg"
        ["auctions"]=>
        string(8) "Auctions"
        ["head"]=>
        string(4) "Head"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

SearchImageByName

  • サンプルコード
    function _getImageByName()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new ImageSearch\SearchImageByNameRequest();
        // Required. The name of the Image Search instance.
        $request->instanceName = "xxxxxxx";
        // Required. An image is uniquely identified by the values of the ProductId and PicName parameters.
        $request->productId = "test";
        $request->picName = "test";
          
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->SearchImageByName($request, $runtime);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        } catch (Exception $e) {
        }
    }
  • レスポンスの例
    ["requestId"]=>
      string(36) "9248A5FC-97AB-4A96-8764-C771CF61D9A1"
      ["success"]=>
      bool(true)
      ["code"]=>
      int(0)
      ["msg"]=>
      string(7) "success"
      ["auctions"]=>
      array(5) {
        [0]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(7) "vae.jpg"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5)
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(21)
      }
      ["picInfo"]=>
      array(2) {
        ["CategoryId"]=>
        int(0)
        ["AllCategories"]=>
        array(14) {
          [0]=>
          array(2) {
            ["Id"]=>
            int(0)
            ["Name"]=>
            string(4) "Tops"
          }
          ........
          [13]=>
          array(2) {
            ["Id"]=>
            int(88888888)
            ["Name"]=>
            string(5) "Other"
          }
        }
      }
      ["_name":protected]=>
      array(7) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["code"]=>
        string(4) "Code"
        ["msg"]=>
        string(3) "Msg"
        ["auctions"]=>
        string(8) "Auctions"
        ["head"]=>
        string(4) "Head"
        ["picInfo"]=>
        string(7) "PicInfo"
      }
      ["_required":protected]=>
      array(0) {
      }
    }

Delete

  • サンプルコード
    function _deleteImage()
    {
        $config = new Config();
        $config->accessKeyId = "<accessKeyId>";
        $config->accessKeySecret = "<accessKeySecret>";
        $config->regionId = "<regionId>";
        $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
        $client = new ImageSearch($config);
        $request = new ImageSearch\DeleteImageRequest();
        // Required. The name of the Image Search instance.
        $request->instanceName = "xxxxxx";
        // Required. The image name, which can be up to 512 characters in length.
    // 1. An image is uniquely identified by the values of the ProductId and PicName parameters.
        $request->productId = "test";
        // 2. Optional. The image name. If this parameter is not specified, the system deletes all the images that correspond to the specified ProductId parameter. If this parameter is specified, the system deletes only the image that is specified by the ProductId and PicName parameters.
        $request->picName = "test";
          
        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 3000;
        $runtime->readTimeout = 3000;
        try {
            $response = $client->deleteImage($request, $runtime);
            var_dump($response->toMap());
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
          } catch (Exception $e) {
        }
    }
  • レスポンスの例
    {
      ["requestId"]=>
      string(36) "052ECD87-933D-4E65-94F7-F0C0947E41A1"
      ["success"]=>
      bool(true)
      ["message"]=>
      string(7) "success"
      ["code"]=>
      int(0)
      ["_name":protected]=>
      array(4) {
        ["requestId"]=>
        string(9) "RequestId"
        ["success"]=>
        string(7) "Success"
        ["message"]=>
        string(7) "Message"
        ["code"]=>
        string(4) "Code"
      }
      ["_required":protected]=>
      array(0) {
      }
    }