本文介绍淘宝联盟服务PHP SDK的使用方法及示例。
接口列表
接口名称 | 接口说明 |
SearchByPic | 用于向淘宝联盟版在线调用,根据图片查询。 |
SearchByUrl | 用于向淘宝联盟版在线调用,根据图片URL查询。 |
GetProductInfoByIds | 用于向淘宝联盟版在线调用,根据ID查询商品。 |
准备工作
在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见
composer镜像
https://developer.aliyun.com/composer
安装依赖
使用composer,获取SDK
composer require alibabacloud/imagesearch-20210501
使用composer.json文件
{
"require": {
"alibabacloud/imagesearch-20210501": "^1.2.1"
}
}
SearchByPic接口
代码示例
<?php
declare (strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByPicAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByUrlRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\ImageSearch;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Psr7\Stream;
function _client(){
$config = new Config();
//阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
//强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
//本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
$config->accessKeyId = getenv("CC_AK_ENV");
$config->accessKeySecret = getenv("CC_SK_ENV");
$config->regionId = "cn-shanghai";
// $config->type = "access_key";
$config->endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
$client = new ImageSearch($config);
return $client;
}
// 根据图片查询
function _searchPic()
{
$request = new SearchByPicAdvanceRequest();
// 必填,图片URL
$request->picContentObject = new Stream(fopen("C:\\xxx/9.jpg", 'r+'));
// 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
// $request->fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
// 必填,PID
$request->pid = "mm_xx_xx_xx";
// 选填,图片类目
//$request->categoryId = 88888888;
// 选填,是否需要进行主体识别。默认true
//$request->crop = false;
// 选填,图片的主体区域,格式为x1,x2,y1,y2
//$request->region = "518,1524,398,1632";
// 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
$request->start = 2;
// 选填,返回结果的数目。取值范围:1-20。默认值:10。
$request->num = 1;
// 选填,渠道ID。用于淘宝联盟中的渠道区分。
//$request->relationId = 1145;
$runtime = new RuntimeOptions();
try {
$response = _client()->searchByPicAdvance($request, $runtime);
var_dump($response->toMap());
} catch (TeaUnableRetryError $e) {
var_dump($e->getLastException(), $e->getLastRequest());
}
}
echo _searchPic();
?>
结果示例
array(2) {
["headers"]=>
array(9) {
["Date"]=>
array(1) {
[0]=>
string(29) "Tue, 08 Jun 2021 10:36:44 GMT"
}
["Content-Type"]=>
array(1) {
[0]=>
string(30) "application/json;charset=utf-8"
}
["Content-Length"]=>
array(1) {
[0]=>
string(3) "933"
}
["Connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["Access-Control-Allow-Origin"]=>
array(1) {
[0]=>
string(1) "*"
}
["Access-Control-Allow-Methods"]=>
array(1) {
[0]=>
string(18) "POST, GET, OPTIONS"
}
["Access-Control-Allow-Headers"]=>
array(1) {
[0]=>
string(57) "X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx"
}
["Access-Control-Max-Age"]=>
array(1) {
[0]=>
string(6) "172800"
}
["x-acs-request-id"]=>
array(1) {
[0]=>
string(36) "01570329-856A-4923-8B98-FC2EB798795E"
}
}
["body"]=>
array(5) {
["Success"]=>
bool(true)
["Code"]=>
int(0)
["Data"]=>
array(1) {
["Auctions"]=>
array(1) {
[0]=>
array(2) {
["Result"]=>
array(8) {
["Title"]=>
string(76) "皮肤科用药"
["PicUrl"]=>
string(71) "//img.alicdn.com/i2xxxx.jpg"
["ReservePrice"]=>
string(3) "7.8"
["ZkFinalPrice"]=>
string(3) "7.8"
["UserType"]=>
int(1)
["Provcity"]=>
string(13) "山东枣庄"
["Url"]=>
string(249) "//s.click.taobao.com/tyyyy"
}
["RankScore"]=>
float(0.7065)
}
}
}
["RequestId"]=>
string(36) "01570329-856A-4923-8B98-FC2EB798795E"
["PicInfo"]=>
array(2) {
["MainRegion"]=>
array(2) {
["Region"]=>
string(16) "269,1212,253,717"
["MultiCategoryId"]=>
array(4) {
[0]=>
array(2) {
["CategoryId"]=>
string(8) "88888888"
["Score"]=>
float(0.91438)
}
[1]=>
array(2) {
["CategoryId"]=>
string(2) "22"
["Score"]=>
float(0.01744)
}
[2]=>
array(2) {
["CategoryId"]=>
string(1) "7"
["Score"]=>
float(0.00839)
}
[3]=>
array(2) {
["CategoryId"]=>
string(1) "8"
["Score"]=>
float(0.00767)
}
}
}
["MultiRegion"]=>
array(1) {
[0]=>
array(1) {
["Region"]=>
string(16) "269,1212,253,717"
}
}
}
}
}
SearchByUrl接口
代码示例
<?php
declare (strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByPicAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByUrlRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\ImageSearch;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Psr7\Stream;
function _client(){
$config = new Config();
//阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
//强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
//本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
$config->accessKeyId = getenv("CC_AK_ENV");
$config->accessKeySecret = getenv("CC_SK_ENV");
$config->regionId = "cn-shanghai";
// $config->type = "access_key";
$config->endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
$client = new ImageSearch($config);
return $client;
}
function _searchUrl()
{
$request = new SearchByUrlRequest();
// 必填,图片URL
$request->picUrl = "https://xxx.oss-cn-shanghai.aliyuncs.com/m.jpg";
// 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
$request->fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
// 必填,PID
$request->pid="m_xx_xx_xx";
// 选填, 图片类目
//$request->categoryId = 88888888;
// 选填,是否需要进行主体识别。默认true
//$request->crop = false;
// 选填,图片的主体区域,格式为x1,x2,y1,y2
//$request->region = "518,1524,398,1632";
// 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
$request->start = 0;
// 选填,返回结果的数目。取值范围:1-20。默认值:10。
$request->num = 1;
// 选填,渠道ID。用于淘宝联盟中的渠道区分。
//$request->relationId = 1145;
$runtime = new RuntimeOptions();
try {
// $response = _client()->searchByUrl($request);
$response = _client()->searchByUrlWithOptions($request,$runtime);
var_dump($response->toMap());
} catch (TeaUnableRetryError $e) {
var_dump($e->getLastException(), $e->getLastRequest());
}
}
echo _searchUrl();
?>
结果示例
array(2) {
["headers"]=>
array(9) {
["Date"]=>
array(1) {
[0]=>
string(29) "Tue, 08 Jun 2021 10:36:44 GMT"
}
["Content-Type"]=>
array(1) {
[0]=>
string(30) "application/json;charset=utf-8"
}
["Content-Length"]=>
array(1) {
[0]=>
string(3) "933"
}
["Connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["Access-Control-Allow-Origin"]=>
array(1) {
[0]=>
string(1) "*"
}
["Access-Control-Allow-Methods"]=>
array(1) {
[0]=>
string(18) "POST, GET, OPTIONS"
}
["Access-Control-Allow-Headers"]=>
array(1) {
[0]=>
string(57) "X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx"
}
["Access-Control-Max-Age"]=>
array(1) {
[0]=>
string(6) "172800"
}
["x-acs-request-id"]=>
array(1) {
[0]=>
string(36) "01570329-856A-4923-8B98-FC2EB798795E"
}
}
["body"]=>
array(5) {
["Success"]=>
bool(true)
["Code"]=>
int(0)
["Data"]=>
array(1) {
["Auctions"]=>
array(1) {
[0]=>
array(2) {
["Result"]=>
array(8) {
["Title"]=>
string(76) "皮肤科用药"
["PicUrl"]=>
string(71) "//img.alicdn.com/i2/xxxxxc.jpg"
["ZkFinalPrice"]=>
string(3) "7.8"
["ReservePrice"]=>
string(3) "7.8"
["UserType"]=>
int(1)
["Provcity"]=>
string(13) "山东枣庄"
["Url"]=>
string(249) "//s.click.taobao.com/t?yyyy"
}
["RankScore"]=>
float(0.7065)
}
}
}
["RequestId"]=>
string(36) "01570329-856A-4923-8B98-FC2EB798795E"
["PicInfo"]=>
array(2) {
["MainRegion"]=>
array(2) {
["Region"]=>
string(16) "269,1212,253,717"
["MultiCategoryId"]=>
array(4) {
[0]=>
array(2) {
["CategoryId"]=>
string(8) "88888888"
["Score"]=>
float(0.91438)
}
[1]=>
array(2) {
["CategoryId"]=>
string(2) "22"
["Score"]=>
float(0.01744)
}
[2]=>
array(2) {
["CategoryId"]=>
string(1) "7"
["Score"]=>
float(0.00839)
}
[3]=>
array(2) {
["CategoryId"]=>
string(1) "8"
["Score"]=>
float(0.00767)
}
}
}
["MultiRegion"]=>
array(1) {
[0]=>
array(1) {
["Region"]=>
string(16) "269,1212,253,717"
}
}
}
}
}
GetProductInfoByIds接口
代码示例
<?php
declare (strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\GetProductInfoByIdsRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\ImageSearch;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Darabonba\OpenApi\Models\Config;
function _client(){
$config = new Config();
//阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
//强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
//本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
$config->accessKeyId = getenv("CC_AK_ENV");
$config->accessKeySecret = getenv("CC_SK_ENV");
$config->regionId = "cn-shanghai";
// $config->type = "access_key";
$config->endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
$client = new ImageSearch($config);
return $client;
}
// 根据ID商品查询
function _getProductInfo()
{
$request = new GetProductInfoByIdsRequest();
// 必填,PID
$request->pid = "xxxxx";
// 必填商品ID串,用','分割,最大40个。
$request->itemIds = "xxxx";
// 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
$request->fields = "xxxx";
try {
$response = _client()->getProductInfoByIds($request);
var_dump($response->toMap());
} catch (TeaUnableRetryError $e) {
var_dump($e->getLastException(), $e->getLastRequest());
}
}
echo _getProductInfo();
?>
结果示例
array(2) {
["headers"]=>
array(10) {
["Date"]=>
array(1) {
[0]=>
string(29) "Mon, 08 Aug 2022 09:43:36 GMT"
}
["Content-Type"]=>
array(1) {
[0]=>
string(30) "application/json;charset=utf-8"
}
["Content-Length"]=>
array(1) {
[0]=>
string(3) "757"
}
["Connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["Access-Control-Allow-Origin"]=>
array(1) {
[0]=>
string(1) "*"
}
["Access-Control-Allow-Methods"]=>
array(1) {
[0]=>
string(31) "POST, GET, OPTIONS, PUT, DELETE"
}
["Access-Control-Allow-Headers"]=>
array(1) {
[0]=>
string(112) "X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx, x-acs-action, x-acs-version, x-acs-date, Content-Type"
}
["Access-Control-Max-Age"]=>
array(1) {
[0]=>
string(6) "172800"
}
["x-acs-request-id"]=>
array(1) {
[0]=>
string(36) "xxx-xxxx-xxxx-240966E45837"
}
["x-acs-trace-id"]=>
array(1) {
[0]=>
string(32) "xxxx"
}
}
["body"]=>
array(4) {
["Code"]=>
int(0)
["Data"]=>
array(1) {
["Auctions"]=>
array(1) {
[0]=>
array(1) {
["Result"]=>
array(7) {
["CommissionRate"]=>
string(3) "300"
["MaxCommission"]=>
array(0) {
}
["PicUrl"]=>
string(71) "xxxx.jpg"
["ReservePrice"]=>
string(2) "35"
["Title"]=>
string(131) "xxxxx辊"
["Url"]=>
string(330) "xxxx"
["ZkFinalPrice"]=>
string(2) "28"
}
}
}
}
["RequestId"]=>
string(36) "xxxx-994E-xxx-xxx-xxx"
["Success"]=>
bool(true)
}
}
文档内容是否对您有帮助?