mcrock/allegroapi

简化使用allegro REST和SOAP API的库

4.0.8 2018-08-16 15:23 UTC

README

简化使用allegro REST和SOAP API的库

安装

此库应使用composer安装

composer require mcrock/allegroapi

要求

  • PHP >= 7.1(请原谅我,我认为是时候和PHP5说再见了)

REST API使用

使用此库,allegro REST API的使用非常简单。

准备凭证

首先,你应该创建Credentials对象,或者在你自己的类中实现CredentialsInterface。

$credentials = new Credentials([
    'restClientId' => 'yourClientId',
    'restClientSecret' => 'yourClientSecret',
    'restApiKey' => 'yourApiKey',
    'restRedirectUri' => 'yourRedirectUri',
    'soapApiKey' => 'yourSoapApiKey',
]);

获取你的令牌

REST API中的每个操作都需要OAuth令牌。你可以通过此库轻松获取它。

$client = new RestClient($credentials);
$authService = $client->getAuthService();

//now redirect to URL generated from this method:
header('Location: '.$authService->getAuthUrl());

登录Allegro.pl后,你将被重定向到你的RedirectUrl,查询字符串中包含授权代码。接下来是什么?

$code = $_GET['code'];

$token = $authService->getNewToken($code);

你可以通过序列化它或存储其值来存储令牌。你还可以实现TokenInterface以创建满足您需求的解决方案。

如果您的令牌已过期,只需使用此方法生成新的令牌

$newToken = $authService->refreshToken($token);

API方法使用

如何使用的最佳解释将是某些示例,所以这里就是

$request = new Request('GET', 'pricing/offer-quotes', null, ['offer.id' => '123123123']);
$response = $client->sendRequest($token, $request);

var_dump(json_decode((string) $response->getBody()));

通过这种方式,你可以使用所有API方法。另一种方法是使用准备好的请求类。

准备好的请求类位置: 这里 我已经涵盖了几乎所有的REST API方法。我会尽力创建所有来覆盖100%。

当然,你可以创建自己的,只需实现RequestInterface即可。这很简单。

示例

$request = new PutChangePriceCommandRequest('123123123', '123.45');
$response = $client->sendRequest($token, $request);

var_dump(json_decode((string) $response->getBody()));

异常

我创建了两个异常类:InvalidRequestMethodException - 仅意味着你在请求中使用了错误的HTTP方法名称。UnauthorizedClientException - 你应该捕获它,以知道何时刷新令牌。

SOAP API

此库中有一个AllegroApiSoapClient类,你可以使用它,到目前为止我没有时间来完成它。它应该可以正常工作,但我需要一些时间来编写文档。

沙盒

Allegro API沙盒有问题。它几乎从不正常工作,所以如果你想测试你的应用程序,请使用生产API。相信我,没有好理由实现沙盒端点:)

结语

如果你有任何建议或注意到任何问题,请随时报告或联系我。