This topic shows you how to use Image Search SDK for PHP and provides sample code.

Methods

Method Description
Add Adds images.
SearchImageByPic Searches for similar images based on a new image.
SearchImageByName Searches for similar images based on the name of an existing image in the image gallery.
Delete Deletes images.

Before you begin

Obtain the Alibaba Cloud Composer mirror from the following website:
https://developer.aliyun.com/composer
Install dependencies.
  • Obtain Image Search SDK for PHP from the Alibaba Cloud Composer mirror.
    composer require alibabacloud/imagesearch-20201214
  • Use the composer.json file.
    {
       "require": {
           "alibabacloud/imagesearch-20201214": "*"    
        }
    }
The following sample code shows the dependency-related information:
declare (strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20201214\ImageSearch;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\AddImageAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\SearchImageByPicAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\SearchImageByNameRequest;
use AlibabaCloud\SDK\ImageSearch\V20201214\Models\DeleteImageRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use AlibabaCloud\Tea\Rpc\Rpc\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

Add

  • Sample code
    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 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 values 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());
        } 
    }
  • Sample responses
    {
      ["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

  • Sample code
    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 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) {
        }
    }
  • Sample responses
    {
      ["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"
          ["Score"]=>
          float(1)
        }
        [1]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "test"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5)
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(171)
      }
      ["picInfo"]=>
      array(3) {
        ["CategoryId"]=>
        int(0)
        ["MultiRegion"]=>
          array(1) {
            [0]=>
            array(1) {
              ["Region"]=>
              string(15) "134,705,187,785"
            }
         }
        ["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

  • Sample code
    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 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) {
        }
    }
  • Sample responses
    ["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"
          ["Score"]=>
          float(1)
        }
      ........
        [4]=>
        array(4) {
          ["CategoryId"]=>
          int(0)
          ["PicName"]=>
          string(4) "test"
          ["ProductId"]=>
          string(4) "1000"
          ["SortExprValues"]=>
          string(22) "7.33136443711219e+24;0"
          ["Score"]=>
          float(1)
        }
      }
      ["head"]=>
      array(3) {
        ["DocsFound"]=>
        int(5)
        ["DocsReturn"]=>
        int(5)
        ["SearchTime"]=>
        int(21)
      }
      ["picInfo"]=>
      array(2) {
        ["CategoryId"]=>
        int(0)
        ["MultiRegion"]=>
          array(1) {
            [0]=>
            array(1) {
              ["Region"]=>
              string(15) "134,705,187,785"
            }
        }
        ["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

  • Sample code
    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 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) {
        }
    }
  • Sample responses
    {
      ["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) {
      }
    }