bbcreation / allegro-api
从ircykk的Allegro API客户端分支,增加对Laravel 6的支持
v1.22.0
2019-12-09 17:33 UTC
Requires
- php: >=5.6
- php-http/client-common: ^1.6
- php-http/discovery: ^1.0
- php-http/httplug: ^1.1
- psr/cache: ^1.0
- psr/http-message: ^1.0
- ramsey/uuid: ^3.0
Requires (Dev)
- phpunit/phpunit: ^5.7
README
Allegro API客户端库,使用PHP编写。
要求
- PHP >= 5.6 || PHP >= 7.0
- HTTP客户端
功能
- REST和SOAP WebApi
- 沙盒支持
- 自动完成
- PSR兼容
安装
通过Composer
composer require ircykk/allegro-api
库建立在HTTPlug之上,我们需要安装HTTP客户端。
composer require php-http/guzzle6-adapter "^1.1"
开发者文档
https://developer.allegro.pl/documentation/
客户端使用方法
使用OAuth进行认证
<?php // Composer autoload require_once __DIR__.'/vendor/autoload.php'; $credentials = ... $client = new \bbcreation\AllegroApi\Client($credentials); // Redirect to allegro for authenticate and get back with code if (!isset($_GET['code'])) { header('Location: '.$client->getAuthUrl()); } else { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); // Store access token... }
我们有$token->access_token来认证我们未来的所有请求。
见示例。
客户端凭据流
为了访问公共资源,例如类别或商品,使用客户端凭据流
$token = $client->fetchAccessTokenWithClientCredentials();
设备流
要使用无浏览器的设备流,请使用getAuthUserCode()方法获取user_code和验证URI
$code = $client->getAuthUserCode();
然后用户认证设备后,您可以获取acces_token
$code = ... $token = $client->fetchAccessTokenWithDeviceCode($code->device_code);
发出请求
<?php // Composer autoload require_once __DIR__.'/vendor/autoload.php'; $credentials = ... $token = ... $client = new \bbcreation\AllegroApi\Client($credentials); $client->authenticate($token); $categories = $client->sale()->categories()->all();
发出SOAP请求
$credentials = ... // WebApi SOAP client $soapClient = new \bbcreation\AllegroApi\WebapiClient($credentials); $categories = $soapClient->webApi()->getCatsDataLimit(0, 10);
沙盒
为了使用沙盒环境,只需将Credentials属性的$sandbox设置为true。
$credentials = new \bbcreation\AllegroApi\Credentials( ... true // Sandbox );
缓存使用
使用任何PSR-6兼容的库来缓存请求。
在此示例中,我们使用Symfony Cache,只需运行即可安装
$ composer require symfony/cache
$credentials = ... $client = new Client($credentials); $cache = new FilesystemAdapter(); $client->addCache($cache, ['default_ttl' => 3600]);
见示例。
日志记录器
使用任何PSR-3日志库,例如Monolog,只需运行即可安装
$ composer require monolog/monolog
$credentials = ... $client = new Client($credentials); $logger = new Logger('api'); $logger->pushHandler( new StreamHandler(__DIR__.'/api.log', Logger::DEBUG) ); $loggerPlugin = new LoggerPlugin($logger); $client->addPlugin($loggerPlugin);
见示例。
自定义
感谢HTTPlug库,可以轻松自定义,例如,要设置语言,请使用HeaderDefaultsPlugin插件
... $headerDefaultsPlugin = new HeaderDefaultsPlugin([ 'Accept-Language' => 'en-US' ]); $client->addPlugin($headerDefaultsPlugin);
待办事项
- 测试
- 文档
贡献
欢迎贡献。
鸣谢
API客户端建立在HTTPlug之上,并受到KnpLabs GitHub客户端的启发。
由wsdl2phpgenerator库生成的Soap类型。