全部产品
云市场

使用说明

更新时间:2018-04-27 22:35:44

前提条件

  • 获取你的 AK ( accessKeyId / accessKeySecret )。链接
  • 获取你的接入地址。链接

使用

依赖

PHP >= 5.3

下载

下载地址:链接

使用方法

创建Client

  1. chdir(dirname(__FILE__));
  2. require_once "../../../aliyun-php-sdk-core/Config.php";
  3. $product = "ImageSearch";
  4. $region = "<Region>"; // 实例的地域信息,如: cn-shanghai(华东2), ap-southeast-1(亚太东南1)
  5. $accessKeyId = "<AccessKeyID>"; // 获取地址: https://ak-console.aliyun.com
  6. $accessKeySecret = "<AccessKeySecret>";
  7. $instanceName = "<InstanceName>"; // 购买的图像搜索实例名称,如: imagesearchtest
  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);

调用API

增加图片

说明:增加图片时,商品图片搜索实例需要明确指定类目 ID,通用图片搜索实例请不要指定类目 ID。

  1. use ImageSearch\Request\V20180120\AddItemRequest;
  2. $req = new AddItemRequest();
  3. $req->setinstanceName($instanceName);
  4. // 读取文件内容
  5. $sampleFileName = "./1.jpg";
  6. $content = file_get_contents($sampleFileName);
  7. $req->setCateId("0");
  8. $req->setItemId("1234");
  9. $req->setCustContent("{\"key\":\"value\"}");
  10. $req->addPicture("cloth1", $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. }

查询图片

说明:查询图片时,对于商品图像搜索,用户可以选择指定或者不指定类目 Id,指定了则会从用户指定的类目 Id 下去进行搜索,不指定则会从系统预测出的类目 Id下去进行搜索;对于通用图片搜索,用户不需要指定类目 Id。

  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. }

删除图片

  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. }