本文介紹Image Search服務PHP SDK的使用方法及樣本。

介面列表

介面名稱 介面說明
Add 增加圖片。
SearchImageByPic 使用圖片進行搜尋。
SearchImageByName 指定名稱,使用已入庫的圖片進行搜尋。
Delete 刪除圖片。

準備工作

composer鏡像
https://developer.aliyun.com/composer
安裝依賴
  • 使用Composer,擷取SDK。
    composer require alibabacloud/imagesearch-20200212
  • 使用composer.json檔案。
    {
       "require": {
           "alibabacloud/imagesearch-20200212": "*"    
        }
    }
依賴相關程式碼範例如下。
declare (strict_types=1);
require __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();
        // 必填,Image Search執行個體名稱。
        $request->instanceName = "xxxxxx";
        // 必填,圖片名稱,最多支援 512個字元。
        // 1. ProductId + PicName唯一確定一張圖片。
        // 2. 如果多次添加圖片具有相同的ProductId + PicName,以最後一次添加為準,前面添加的圖片將被覆蓋。
        $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();
        // 必填,Image Search執行個體名稱。
        $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();
        // 必填,Image Search執行個體名稱。
        $request->instanceName = "xxxxxxx";
        // 必填,ProductId + PicName唯一確定一張圖片
        $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();
        // 必填,Image Search執行個體名稱。
        $request->instanceName = "xxxxxx";
        // 必填,圖片名稱,最多支援 512個字元。
    // 1. ProductId + PicName唯一確定一張圖片。
        $request->productId = "test";
        // 2. 選填,圖片名稱。若不指定本參數,則刪除ProductId下所有圖片;若指定本參數,則刪除ProductId+PicName指定的圖片。
        $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) {
      }
    }