This topic describes how to use Alibaba Cloud Image Search SDKs for PHP.
Preparations
- Before you install and use Alibaba Cloud SDKs, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Obtain an AccessKey pair.
- Download and install the Alibaba Cloud Image Search SDK for PHP. The PHP version must be 5.3 or later.
Create a client
The following sample code is used to create a client:
chdir(dirname(__FILE__));
require_once ".. /.. /../aliyun-php-sdk-core/Config.php";
$product = "ImageSearch";
$region = "<Region>"; // The region ID of the instance. For example, if your instance resides in the China (Shanghai) region, set this parameter to cn-shanghai. If your instance resides in the Singapore (Singapore) region, set this parameter to ap-southeast-1.
$accessKeyId = "<AccessKeyID>"; // The AccessKey ID. You can obtain the AccessKey ID from https://ak-console.aliyun.com.
$accessKeySecret = "<AccessKeySecret>";
$instanceName = "<InstanceName>"; // The name of the purchased Image Search instance, for example, imagesearchtest.
$domain = "imagesearch.".$region.".aliyuncs.com";
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
DefaultProfile::addEndPoint($region, $region, $product, $domain);
$client = new DefaultAcsClient($profile);
Add images
For product image searches, you can specify or do not specify category IDs. For generic
image searches, you do not need to specify category IDs.
use ImageSearch\Request\V20180120\AddItemRequest;
$req = new AddItemRequest();
$req->setinstanceName($instanceName);
// Read the image file content.
$sampleFileName = ". /1.jpg";
$content = file_get_contents($sampleFileName);
$req->setCateId("0");
$req->setItemId("1234");
$req->setCustContent("{\"key\":\"value\"}");
$req->addPicture("cloth1", $content);
if (! $req->buildPostContent()) {
return ;
}
try {
$response = $client->getAcsResponse($req);
print_r($response);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
Search for images
For product image searches, you can specify or do not specify category IDs. If you
specify a category ID, the system searches for images based on the specified category
ID. If you do not specify a category ID, the system predicts the category ID and searches
for images based on the predicted category ID. For generic image searches, you do
not need to specify category IDs.
use ImageSearch\Request\V20180120\SearchItemRequest;
$req = new SearchItemRequest();
$req->setinstanceName($instanceName);
$req->setNum(10);
$req->setStart(0);
// Read Image File
$sampleFileName = ". /1.jpg";
$content = file_get_contents($sampleFileName);
fclose($handle);
$req->setSearchPicture($content);
if (! $req->buildPostContent()) {
return ;
}
try {
$response = $client->getAcsResponse($req);
print_r($response);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
Delete images
The following sample code is used to delete images:
use ImageSearch\Request\V20180120\DeleteItemRequest;
$req = new DeleteItemRequest();
$req->setinstanceName($instanceName);
$req->setItemId("1234");
$req->addPicture("cloth1");
if (! $req->buildPostContent()) {
return ;
}
try {
$response = $client->getAcsResponse($req);
print_r($response);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}