EBAY API PHP开发指南

1、获取access_token

参考文档:https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html
1.1 授权

$scope = array(
			'https://api.ebay.com/oauth/api_scope',
			'https://api.ebay.com/oauth/api_scope/sell.account',
);

// @scope:作用域,参考文档:https://developer.ebay.com/api-docs/static/oauth-scopes.html

$clientId = 目标环境的client_id值;

// @clientId 目标环境的client_id值,参考文档:https://developer.ebay.com/api-docs/static/oauth-credentials.html

$redirectUri = 目标环境的 RuName 值;

// @clientId 设置的重定向地址唯一标志,参考文档:https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html

$responseType = 'code';  

//@responseType 设置为 “code” 可让 eBay 生成并返回授权代码。

$url = 'https://signin.ebay.com/authorize';
$url .= '?client_id=' . $clientId;
$url .= '&redirect_uri=' . $redirectUri; 
$url .= '&response_type=code';
$url .= '&state='
$url .= '&scope=' . implode('%20', $scope),
Header('Location: ' . $url);

跳转授权,同意后会携带CODE跳转回RuName设置的地址

1.2 获取access_token及refresh_token

$body = array(
				'grant_type' => 'authorization_code',
				'code' => $_GET['code'],
				'redirect_uri' => $redirectUri
			);

// @body 请求体
// @body.grant_type
// @body.code 授权携带
// @body.redirect_uri 目标环境的 RuName 值

$uri = 'https://api.ebay.com/identity/v1/oauth2/token';

// 获取token地址

$clientSecret = 目标环境的client_secret值;
$headers = array(
	// 'Accept' => 'application/json',
	// 'Accept-Charset' => 'utf-8',
	// 'X-EBAY-C-MARKETPLACE-ID' => 'EBAY-US',
	'Content-Type' => 'application/x-www-form-urlencoded',
	'Authorization' => 'Basic ' . base64_encode($clientId . ':' . $clientSecret)
);

// @clientSecret 目标环境的client_id值,参考文档:https://developer.ebay.com/api-docs/static/oauth-credentials.html

$config = array(
	'base_uri' => $uri,
	'headers' => $headers,
	'http_errors' => false,
	'verify' => false,
	'body' => http_build_query($body)
);
$client = new GuzzleHttp\Client($config);
$request = $client->request('POST');
$http_status = $request->getStatusCode();
$response = $request->getBody()->getContents();
$response_headers = $request->getHeaders();

// @client 发起请求

if ($http_status >= 200 && $http_status <= 299)
{
	// 请求成功
}
else
{
	// 请求失败
}
if ( ! empty($response))
{
	$response = json_decode($response, true);
	$message = isset($response['errors'][0]['longMessage']) ? $response['errors'][0]['longMessage'] :(isset($response['error_description']) ? $response['error_description'] : '');
}

// 如果请求成功会有以下数据返回

$response = array(
	'access_token' => '',   //令牌
	'expires_in' => '',  // 令牌有效时间
	'refresh_token' => '', // 令牌过期后可用此码获取令牌,而非重新授权
	'refresh_token_expires_in' => '', // refresh_token有效时间,过期后需重新授权
);

2、以refresh_token获取access_token

$body = array(
	'grant_type' => 'refresh_token',
	'refresh_token' => $response['refresh_token'],
	'scope' => implode(' ', $scope)
);
$uri = 'https://api.ebay.com/identity/v1/oauth2/token';
$headers = array(
	// 'Accept' => 'application/json',
	// 'Accept-Charset' => 'utf-8',
	// 'X-EBAY-C-MARKETPLACE-ID' => 'EBAY-US',
	'Content-Type' => 'application/x-www-form-urlencoded',
	'Authorization' => 'Basic ' . base64_encode($clientId . ':' . $clientSecret)
);
$config = array(
	'base_uri' => $uri,
	'headers' => $headers,
	'http_errors' => false,
	'verify' => false,
	'body' => http_build_query($body)
);
$client = new GuzzleHttp\Client($config);
$request = $client->request('POST');
$http_status = $request->getStatusCode();
$response = $request->getBody()->getContents();
$response_headers = $request->getHeaders();

3、API调用(获取账号信息)

$config = array(
	'base_uri' => 'https://apiz.ebay.com/commerce/identity/v1/user/',
	'headers' => array(
		'Authorization' => 'Bearer ' . $response['access_token']
	),
	'verify' => false
);
$client = new GuzzleHttp\Client($config);
$request = $client->request('GET');
$response = $request->getBody()->getContents();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值