All Products
Search
Document Center

Instructions

Last Updated: Feb 20, 2019
  • Obtain your AccessKey (AccessKey ID/AccessKey Secret): Link
  • Obtain your endpoint: Link

Dependency

PHP >= 5.3

Download

Download address: Link.

Create a client

  1. chdir(dirname(__FILE__));
  2. require_once "../../../aliyun-php-sdk-core/Config.php";
  3. $product = "ImageSearch";
  4. $region = "<Region>"; // Instance region information, for example, cn-shanghai (Shanghai), ap-southeast-1 (Singapore).
  5. $accessKeyId = "<AccessKeyID>"; // Address: https://ak-console.aliyun.com
  6. $accessKeySecret = "<AccessKeySecret>";
  7. $instanceName = "<InstanceName>"; // Name of the image search instance that you have purchased, for example, imagesearchtest.t
  8. $domain = "imagesearch.".$region.".aliyuncs.com";
  9. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  10. DefaultProfile::addEndPoint($region, $region, $product, $domain);
  11. $client = new DefaultAcsClient($profile);

Run API

Add images

For product search instances, category IDs are not necessary. For generic search instances, no category ID is required.

  1. use ImageSearch\Request\V20180120\AddItemRequest;
  2. $req = new AddItemRequest();
  3. $req->setinstanceName($instanceName);
  4. $sampleFileName = "./1.jpg";
  5. $content = file_get_contents($sampleFileName);
  6. $req->setCateId("0");
  7. $req->setItemId("1234");
  8. $req->setCustContent("{\"key\":\"value\"}");
  9. $req->addPicture("cloth1", $content);
  10. if (!$req->buildPostContent()) {
  11. return ;
  12. }
  13. try {
  14. $response = $client->getAcsResponse($req);
  15. print_r($response);
  16. } catch(ServerException $e) {
  17. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  18. } catch(ClientException $e) {
  19. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  20. }

Search images

When you use product search, you can specify a category ID. The system will perform a lookup only on products that belong to the specified category. If you do not specify a category ID, the system will determine the category for you. For generic search, no category ID is required.

  1. use ImageSearch\Request\V20180120\SearchItemRequest;
  2. $req = new SearchItemRequest();
  3. $req->setinstanceName($instanceName);
  4. $req->setNum(10);
  5. $req->setStart(0);
  6. // Read Image File
  7. $sampleFileName = "./1.jpg";
  8. $content = file_get_contents($sampleFileName);
  9. fclose($handle);
  10. $req->setSearchPicture($content);
  11. if (!$req->buildPostContent()) {
  12. return ;
  13. }
  14. try {
  15. $response = $client->getAcsResponse($req);
  16. print_r($response);
  17. } catch(ServerException $e) {
  18. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  19. } catch(ClientException $e) {
  20. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  21. }

Delete images

  1. use ImageSearch\Request\V20180120\DeleteItemRequest;
  2. $req = new DeleteItemRequest();
  3. $req->setinstanceName($instanceName);
  4. $req->setItemId("1234");
  5. $req->addPicture("cloth1");
  6. if (!$req->buildPostContent()) {
  7. return ;
  8. }
  9. try {
  10. $response = $client->getAcsResponse($req);
  11. print_r($response);
  12. } catch(ServerException $e) {
  13. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  14. } catch(ClientException $e) {
  15. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  16. }