全部产品
云市场

PHP SDK

更新时间:2020-05-12 12:05:10

接口列表

接口名称 接口说明
Add 增加图片
SearchImageByPic 使用图片进行搜索
SearchImageByName 指定名称,使用已入库的图片进行搜索
Delete 删除图片

准备工作

依赖安装

方法1: 使用Composer,获取SDK:(版本0.2.11)

  1. composer require alibabacloud/imagesearch-20200212

方法2: 使用composer.json文件:

  1. {
  2. "require": {
  3. "alibabacloud/imagesearch-20200212": "^0.2.11" }
  4. }

代码示例

依赖相关
  1. declare (strict_types=1);
  2. require __DIR__ . '/vendor/autoload.php';
  3. use AlibabaCloud\SDK\ImageSearch\V20200212\ImageSearch;
  4. use AlibabaCloud\SDK\OpenPlatform\V20191219\OpenPlatform;
  5. use AlibabaCloud\SDK\OpenPlatform\V20191219\OpenPlatform\AuthorizeFileUploadRequest;
  6. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  7. use AlibabaCloud\Tea\FileForm\FileForm\FileField;
  8. use AlibabaCloud\Tea\Rpc\Rpc\Config;
  9. use AlibabaCloud\Tea\RpcUtils\RpcUtils;
  10. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  11. use GuzzleHttp\Psr7\Stream;

Add接口

代码示例
  1. function _addImage()
  2. {
  3. $config = new Config();
  4. $config->accessKeyId = "<accessKeyId>";
  5. $config->accessKeySecret = "<accessKeySecret>";
  6. $config->regionId = "<regionId>";
  7. $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
  8. $client = new ImageSearch($config);
  9. $request = new ImageSearch\AddImageAdvanceRequest();
  10. // 必填,图像搜索实例名称。
  11. $request->instanceName = "xxxxxx";
  12. // 必填,图片名称,最多支持 512个字符。
  13. // 1. ProductId + PicName唯一确定一张图片。
  14. // 2. 如果多次添加图片具有相同的ProductId + PicName,以最后一次添加为准,前面添加的的图片将被覆盖。
  15. $request->productId = "test";
  16. $request->picName = "test";
  17. $request->picContentObject = new Stream(fopen("<filePath>", 'r+'));
  18. $runtime = new RuntimeOptions();
  19. $runtime->maxIdleConns = 3;
  20. $runtime->connectTimeout = 3000;
  21. $runtime->readTimeout = 3000;
  22. try {
  23. $response = $client->addImageAdvance($request, $runtime);
  24. var_dump($response->toMap());
  25. } catch (TeaUnableRetryError $e) {
  26. var_dump($e->getLastException(), $e->getLastRequest());
  27. }
  28. }
返回结果示例
  1. {
  2. ["requestId"]=>
  3. string(36) "242CB8B7-DD36-42ED-9A76-37915DC7ED81"
  4. ["success"]=>
  5. bool(true)
  6. ["message"]=>
  7. string(7) "success"
  8. ["code"]=>
  9. int(0)
  10. ["picInfo"]=>
  11. array(2) {
  12. ["CategoryId"]=>
  13. int(0)
  14. ["Region"]=>
  15. string(15) "167,477,221,407"
  16. }
  17. ["_name":protected]=>
  18. array(5) {
  19. ["requestId"]=>
  20. string(9) "RequestId"
  21. ["success"]=>
  22. string(7) "Success"
  23. ["message"]=>
  24. string(7) "Message"
  25. ["code"]=>
  26. string(4) "Code"
  27. ["picInfo"]=>
  28. string(7) "PicInfo"
  29. }
  30. ["_required":protected]=>
  31. array(0) {
  32. }
  33. }

SearchImageByPic接口

代码示例
  1. function _getImageByPic()
  2. {
  3. $config = new Config();
  4. $config->accessKeyId = "<accessKeyId>";
  5. $config->accessKeySecret = "<accessKeySecret>";
  6. $config->regionId = "<regionId>";
  7. $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
  8. $client = new ImageSearch($config);
  9. $request = new ImageSearch\SearchImageByPicAdvanceRequest();
  10. // 必填,图像搜索实例名称。
  11. $request->instanceName = "xxxxx";
  12. $imageStream = new Stream(fopen("<filePath>", 'r+'));
  13. $request->picContentObject = $imageStream;
  14. $runtime = new RuntimeOptions();
  15. $runtime->maxIdleConns = 3;
  16. $runtime->connectTimeout = 3000;
  17. $runtime->readTimeout = 3000;
  18. try {
  19. $response = $client->searchImageByPicAdvance($request, $runtime);
  20. var_dump($response->toMap());
  21. } catch (TeaUnableRetryError $e) {
  22. var_dump($e->getLastException(), $e->getLastRequest());
  23. } catch (Exception $e) {
  24. }
  25. }
返回结果示例
  1. {
  2. ["requestId"]=>
  3. string(36) "453517A3-7D00-476E-A946-8CC3B9DC6FA1"
  4. ["success"]=>
  5. bool(true)
  6. ["code"]=>
  7. int(0)
  8. ["msg"]=>
  9. string(7) "success"
  10. ["auctions"]=>
  11. array(5) {
  12. [0]=>
  13. array(4) {
  14. ["CategoryId"]=>
  15. int(0)
  16. ["PicName"]=>
  17. string(7) "vae.jpg"
  18. ["ProductId"]=>
  19. string(4) "1000"
  20. ["SortExprValues"]=>
  21. string(22) "7.33136443711219e+24;0"
  22. }
  23. [1]=>
  24. array(4) {
  25. ["CategoryId"]=>
  26. int(0)
  27. ["PicName"]=>
  28. string(4) "test"
  29. ["ProductId"]=>
  30. string(4) "test"
  31. ["SortExprValues"]=>
  32. string(22) "7.33136443711219e+24;0"
  33. }
  34. ........
  35. [4]=>
  36. array(4) {
  37. ["CategoryId"]=>
  38. int(0)
  39. ["PicName"]=>
  40. string(4) "test"
  41. ["ProductId"]=>
  42. string(4) "1000"
  43. ["SortExprValues"]=>
  44. string(22) "7.33136443711219e+24;0"
  45. }
  46. }
  47. ["head"]=>
  48. array(3) {
  49. ["DocsFound"]=>
  50. int(5)
  51. ["DocsReturn"]=>
  52. int(5)
  53. ["SearchTime"]=>
  54. int(171)
  55. }
  56. ["picInfo"]=>
  57. array(3) {
  58. ["CategoryId"]=>
  59. int(0)
  60. ["AllCategories"]=>
  61. array(14) {
  62. [0]=>
  63. array(2) {
  64. ["Id"]=>
  65. int(0)
  66. ["Name"]=>
  67. string(4) "Tops"
  68. }
  69. ........
  70. [13]=>
  71. array(2) {
  72. ["Id"]=>
  73. int(88888888)
  74. ["Name"]=>
  75. string(5) "Other"
  76. }
  77. }
  78. ["Region"]=>
  79. string(15) "167,477,221,407"
  80. }
  81. ["_name":protected]=>
  82. array(7) {
  83. ["requestId"]=>
  84. string(9) "RequestId"
  85. ["success"]=>
  86. string(7) "Success"
  87. ["code"]=>
  88. string(4) "Code"
  89. ["msg"]=>
  90. string(3) "Msg"
  91. ["auctions"]=>
  92. string(8) "Auctions"
  93. ["head"]=>
  94. string(4) "Head"
  95. ["picInfo"]=>
  96. string(7) "PicInfo"
  97. }
  98. ["_required":protected]=>
  99. array(0) {
  100. }
  101. }

SearchImageByName接口

代码示例
  1. function _getImageByName()
  2. {
  3. $config = new Config();
  4. $config->accessKeyId = "<accessKeyId>";
  5. $config->accessKeySecret = "<accessKeySecret>";
  6. $config->regionId = "<regionId>";
  7. $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
  8. $client = new ImageSearch($config);
  9. $request = new ImageSearch\SearchImageByNameRequest();
  10. // 必填,图像搜索实例名称。
  11. $request->instanceName = "xxxxxxx";
  12. // 必填,ProductId + PicName唯一确定一张图片
  13. $request->productId = "test";
  14. $request->picName = "test";
  15. $runtime = new RuntimeOptions();
  16. $runtime->maxIdleConns = 3;
  17. $runtime->connectTimeout = 3000;
  18. $runtime->readTimeout = 3000;
  19. try {
  20. $response = $client->SearchImageByName($request, $runtime);
  21. var_dump($response->toMap());
  22. } catch (TeaUnableRetryError $e) {
  23. var_dump($e->getLastException(), $e->getLastRequest());
  24. } catch (Exception $e) {
  25. }
  26. }
返回结果示例
  1. ["requestId"]=>
  2. string(36) "9248A5FC-97AB-4A96-8764-C771CF61D9A1"
  3. ["success"]=>
  4. bool(true)
  5. ["code"]=>
  6. int(0)
  7. ["msg"]=>
  8. string(7) "success"
  9. ["auctions"]=>
  10. array(5) {
  11. [0]=>
  12. array(4) {
  13. ["CategoryId"]=>
  14. int(0)
  15. ["PicName"]=>
  16. string(7) "vae.jpg"
  17. ["ProductId"]=>
  18. string(4) "1000"
  19. ["SortExprValues"]=>
  20. string(22) "7.33136443711219e+24;0"
  21. }
  22. ........
  23. [4]=>
  24. array(4) {
  25. ["CategoryId"]=>
  26. int(0)
  27. ["PicName"]=>
  28. string(4) "test"
  29. ["ProductId"]=>
  30. string(4) "1000"
  31. ["SortExprValues"]=>
  32. string(22) "7.33136443711219e+24;0"
  33. }
  34. }
  35. ["head"]=>
  36. array(3) {
  37. ["DocsFound"]=>
  38. int(5)
  39. ["DocsReturn"]=>
  40. int(5)
  41. ["SearchTime"]=>
  42. int(21)
  43. }
  44. ["picInfo"]=>
  45. array(2) {
  46. ["CategoryId"]=>
  47. int(0)
  48. ["AllCategories"]=>
  49. array(14) {
  50. [0]=>
  51. array(2) {
  52. ["Id"]=>
  53. int(0)
  54. ["Name"]=>
  55. string(4) "Tops"
  56. }
  57. ........
  58. [13]=>
  59. array(2) {
  60. ["Id"]=>
  61. int(88888888)
  62. ["Name"]=>
  63. string(5) "Other"
  64. }
  65. }
  66. }
  67. ["_name":protected]=>
  68. array(7) {
  69. ["requestId"]=>
  70. string(9) "RequestId"
  71. ["success"]=>
  72. string(7) "Success"
  73. ["code"]=>
  74. string(4) "Code"
  75. ["msg"]=>
  76. string(3) "Msg"
  77. ["auctions"]=>
  78. string(8) "Auctions"
  79. ["head"]=>
  80. string(4) "Head"
  81. ["picInfo"]=>
  82. string(7) "PicInfo"
  83. }
  84. ["_required":protected]=>
  85. array(0) {
  86. }
  87. }

Delete接口

代码示例
  1. function _deleteImage()
  2. {
  3. $config = new Config();
  4. $config->accessKeyId = "<accessKeyId>";
  5. $config->accessKeySecret = "<accessKeySecret>";
  6. $config->regionId = "<regionId>";
  7. $config->endpoint = "imagesearch.[regionId].aliyuncs.com";
  8. $client = new ImageSearch($config);
  9. $request = new ImageSearch\DeleteImageRequest();
  10. // 必填,图像搜索实例名称。
  11. $request->instanceName = "xxxxxx";
  12. // 必填,图片名称,最多支持 512个字符。
  13. // 1. ProductId + PicName唯一确定一张图片。
  14. $request->productId = "test";
  15. // 2. 选填,图片名称。若不指定本参数,则删除ProductId下所有图片;若指定本参数,则删除ProductId+PicName指定的图片。
  16. $request->picName = "test";
  17. $runtime = new RuntimeOptions();
  18. $runtime->maxIdleConns = 3;
  19. $runtime->connectTimeout = 3000;
  20. $runtime->readTimeout = 3000;
  21. try {
  22. $response = $client->deleteImage($request, $runtime);
  23. var_dump($response->toMap());
  24. } catch (TeaUnableRetryError $e) {
  25. var_dump($e->getLastException(), $e->getLastRequest());
  26. } catch (Exception $e) {
  27. }
  28. }
返回结果示例
  1. {
  2. ["requestId"]=>
  3. string(36) "052ECD87-933D-4E65-94F7-F0C0947E41A1"
  4. ["success"]=>
  5. bool(true)
  6. ["message"]=>
  7. string(7) "success"
  8. ["code"]=>
  9. int(0)
  10. ["_name":protected]=>
  11. array(4) {
  12. ["requestId"]=>
  13. string(9) "RequestId"
  14. ["success"]=>
  15. string(7) "Success"
  16. ["message"]=>
  17. string(7) "Message"
  18. ["code"]=>
  19. string(4) "Code"
  20. }
  21. ["_required":protected]=>
  22. array(0) {
  23. }
  24. }