All Products
Search
Document Center

PHP SDK

Last Updated: Jul 05, 2019

Preparations

  1. Before installing and using Alibaba Cloud SDKs, make sure that you have registered an Alibaba Cloud account and generated an AccessKey. For more information, see Create an AccessKey.

  2. Download the Image Search PHP SDK. Click here to download.

Sample code

  1. <?php
  2. //You can reference the Image Search PHP SDK based on your downloaded packages.
  3. require_once "./aliyun-php-sdk-core/Config.php";
  4. use ImageSearch\Request\V20190325\AddImageRequest;
  5. use ImageSearch\Request\V20190325\SearchImageRequest;
  6. use ImageSearch\Request\V20190325\DeleteImageRequest;
  7. DefaultProfile::addEndPoint("<region>", "<region>", "ImageSearch", "imagesearch.<region>.aliyuncs.com");
  8. $profile = DefaultProfile::getProfile("<region>", "<your-access-key-id>", "<your-access-key-secret>");
  9. $client = new DefaultAcsClient($profile);
  10. // Add an image.
  11. $addRequest = new AddImageRequest();
  12. $addRequest->setInstanceName("demo");
  13. $addRequest->setProductId("test");
  14. $addRequest->setPicName("test");
  15. $content = file_get_contents("/home/admin/demo.jpg");
  16. $encodePicContent = base64_encode($content);
  17. $addRequest->setPicContent($encodePicContent);
  18. // (Optional) Indicates whether subject recognition is required. The default value is true.
  19. // 1. If the parameter is set to true, the system identifies the subject and searches for the identified subject. The subject recognition result is included in the response.
  20. // 2. If this parameter is set to false, the entire image is searched for without subject recognition.
  21. //$addRequest->setCrop("false");
  22. //$addRequest->setCrop("true");
  23. // (Optional) The subject area of the image, in the format of x1,x2,y1,y2. Specifically, x1 and y1 determine the point in the upper-left corner, and x2 and y2 determine the point in the lower-right corner.
  24. // If you set the Region parameter, the search is conducted based on this parameter regardless of the value of the Crop parameter.
  25. // $addRequest->setRegion("100,300,100,300");
  26. try {
  27. $addResponse = $client->getAcsResponse($addRequest);
  28. print_r($addResponse);
  29. } catch(ServerException $e) {
  30. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  31. } catch(ClientException $e) {
  32. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  33. }
  34. // Search for an image.
  35. $searchRequest = new SearchImageRequest();
  36. $searchRequest->setInstanceName("demo");
  37. $searchRequest->setType("SearchByName");
  38. $searchRequest->setProductId("test");
  39. $searchRequest->setPicName("test");
  40. try {
  41. $searchResponse = $client->getAcsResponse($searchRequest);
  42. print_r($searchResponse);
  43. } catch(ServerException $e) {
  44. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  45. } catch(ClientException $e) {
  46. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  47. }
  48. // Delete an image.
  49. $deleteRequest = new DeleteImageRequest();
  50. $deleteRequest->setInstanceName("demo");
  51. $deleteRequest->setProductId("test");
  52. try {
  53. $deleteResponse = $client->getAcsResponse($deleteRequest);
  54. print_r($deleteResponse);
  55. } catch(ServerException $e) {
  56. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  57. } catch(ClientException $e) {
  58. print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
  59. }